gpt4 book ai didi

ruby-on-rails - 如何在 has_many :through association 上使用 ransack 实现搜索

转载 作者:行者123 更新时间:2023-12-04 08:06:23 27 4
gpt4 key购买 nike

这是我到目前为止所做的

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com