【问题标题】:Filter users post with user params使用用户参数过滤用户帖子
【发布时间】:2018-11-30 12:08:42
【问题描述】:

我正在开发一个应用程序,并尝试在帖子索引上实现一个带有 filterrific 的过滤器。

过滤器使用用户参数对帖子进行排序:mother_tonguelocalityavailability

  • 我创建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 :userUser has_one :post,这两个选项是相互排斥的。
  • 应该是多对多的关系。因此,您必须更改关联
  • 用户只能创建一个帖子,如何创建用户和帖子之间的关联以利用帖子索引上的 users_params ?
  • 我认为您已经在ApplicationController 中定义了@user。顺便说一句,你在使用设计吗?

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

我认为您不需要用户和帖子之间的连接表,删除它。你的加入应该开始工作。 除此之外,如果您使用的是 devise gem,那么您已经在从ApplicationController 继承的所有控制器中提供了@user。如果不是,那就是完全不同的故事了。研究一下,关于这个话题有很多资源。 不过,请不要犹豫发表评论。

【讨论】:

  • 我删除了连接表,但我有一个新错误,@seethrough 看我楼上的帖子。我不使用设计! ://
猜你喜欢
  • 2020-12-12
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多