作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我到目前为止所做的
class Business < ApplicationRecord
has_many :locations
has_many :continent, through: :locations
...........
class Continent < ApplicationRecord
has_many :locations
has_many :businesses, through: :locations
..............
class Location < ApplicationRecord
belongs_to :continent
belongs_to :business
end
#搜索表单
<%= search_form_for @q do |f| %>
<%= f.select :continent_id_eq, options_from_collection_for_select(Continent.all.order(:name), :id, :name, @q.continent_id_eq), { }, {class: ''} %>
<%= f.search_field :name_cont %>
我已经创建了记录并给出了适当的association.s 表格,当我从列表中选择一个大陆来查询业务时,它不会查询。没有错误显示
localhost:3000/businesses?q[continent_id_eq]=1&q[name_cont]=&commit=Search
这是控制台日志
Business Load (1.6ms) SELECT DISTINCT "businesses".* FROM "businesses" LEFT OUTER JOIN "locations" ON "locations"."business_id" = "businesses"."id" LEFT OUTER JOIN "continents" ON "continents"."id" = "locations"."continent_id" WHERE "continents"."id" = 3
我究竟做错了什么
最佳答案
自 Business
型号 has_many :continents
,您需要使用复数 continents
, 所以:
<%= f.select :continents_id_eq, options_from_collection_for_select(Continent.all.order(:name), :id, :name, @q.continent_id_eq), { }, {class: ''} %>
关于ruby-on-rails - 如何在 has_many :through association 上使用 ransack 实现搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66201157/
我是一名优秀的程序员,十分优秀!