Ruby on rails 如何通过关联实现搜索和搜索
这就是我到目前为止所做的Ruby on rails 如何通过关联实现搜索和搜索,ruby-on-rails,ransack,Ruby On Rails,Ransack,这就是我到目前为止所做的 class Business < ApplicationRecord has_many :locations has_many :continent, through: :locations ........... class Continent < ApplicationRecord has_many :locations has_many :businesses, through: :locations
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
这是控制台日志
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
我做错了什么既然
业务
模型有很多:大陆,您需要使用复数大陆
,因此:
<%= f.select :continents_id_eq, options_from_collection_for_select(Continent.all.order(:name), :id, :name, @q.continent_id_eq), { }, {class: ''} %>
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
<%= f.select :continents_id_eq, options_from_collection_for_select(Continent.all.order(:name), :id, :name, @q.continent_id_eq), { }, {class: ''} %>