Ruby on rails 基于连接对象字段查找记录的更好方法?

Ruby on rails 基于连接对象字段查找记录的更好方法?,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我是rails的新手,我想看看是否有一种更有效的方法可以基于junction对象中的字段集来查找位置。这种关系是,事件通过事件位置有多个位置。下面是一个简化的示例: @locations=[] events_locations = @event.events_locations.where(:fieldvalue=>1) events_locations.each do |el| @locations<<Location.find(el.location_id) end

我是rails的新手,我想看看是否有一种更有效的方法可以基于junction对象中的字段集来查找位置。这种关系是,事件通过事件位置有多个位置。下面是一个简化的示例:

@locations=[]
events_locations = @event.events_locations.where(:fieldvalue=>1)
events_locations.each do |el|
  @locations<<Location.find(el.location_id)
end
@locations=[]
events\u locations=@event.events\u locations.where(:fieldvalue=>1)
活动地点。每个地点都有|

@地点我猜fieldvalue是一个事件和地点的字段,那么

class Event
  has_and_belongs_to_many :locations
end

@locations = @event.locations.where(events_locations: {fieldvalue: 1})

你的假设是正确的,谢谢!你能推荐一些好的网站让我找到这样的片段吗?