【发布时间】:2018-11-30 12:08:42
【问题描述】:
我正在开发一个应用程序,并尝试在帖子索引上实现一个带有 filterrific 的过滤器。
过滤器使用用户参数对帖子进行排序:mother_tongue、locality 和 availability。
- 我创建JoinTableUsersPosts
- 帖子属于_to:用户
- 用户 has_one :post
Post.rb:
filterrific(
default_filter_params: {},
available_filters: [
:with_mother_tongue,
:with_locality,
:with_start_time_gte
]
)
scope :with_mother_tongue, -> (search_string) { joins(:users).where("users.mother_tongue LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
scope :with_locality, -> (search_string) { joins(:users).where("users.locality LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
scope :with_start_time_gte, -> (ref_date) { joins(:availabilities).where('availabilities.start_time >= ?', ref_date) }
过滤器适用于用户索引,但不适用于错误的帖子索引:
无法将“发布”加入名为“用户”的关联;也许你拼错了 是吗?
你知道为什么会出现这个错误吗?
【问题讨论】:
-
这对我来说有点奇怪,你创建
JoinTableUsersPosts并拥有Post belongs_to :user和User has_one :post,这两个选项是相互排斥的。 -
应该是多对多的关系。因此,您必须更改关联
-
用户只能创建一个帖子,如何创建用户和帖子之间的关联以利用帖子索引上的 users_params ?
-
我认为您已经在
ApplicationController中定义了@user。顺便说一句,你在使用设计吗?
标签: ruby-on-rails ruby ruby-on-rails-5