Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 rails 3:使用多对多表的ID执行另一个查询_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 rails 3:使用多对多表的ID执行另一个查询

Ruby on rails 3 rails 3:使用多对多表的ID执行另一个查询,ruby-on-rails-3,Ruby On Rails 3,我有一个带有联系人的表,一个带有事件的表,以及一个带有联系人id和事件id的连接表,以映射它们的多对多关系 我是RoR新手,我习惯于查询第一个表,构建一组id,并在第二个表上运行查询,其中id位于setofids中。在RoR中也是这样吗 以下是我目前掌握的情况: def view @contact = Contact.find(params[:contact_id]) @contactevents = Contacts_Event.where(:contact_id => params

我有一个带有联系人的表,一个带有事件的表,以及一个带有联系人id和事件id的连接表,以映射它们的多对多关系

我是RoR新手,我习惯于查询第一个表,构建一组id,并在第二个表上运行查询,其中id位于setofids中。在RoR中也是这样吗

以下是我目前掌握的情况:

def view
 @contact = Contact.find(params[:contact_id])
 @contactevents = Contacts_Event.where(:contact_id => params[:contact_id])

 s1 = Set.new 
@contactevents.each do |contactevent| 
  s1.add(contactevent.event_id)
end

 @contactevents_test = Event.where(:id => @s1)

end

好的,这是一种方法,但实际上你看到的是
有并且属于许多
协会。这样做的目的是,当您执行
@contact=contact.find(params[:contact\u id])
时,它会自动拉取所有相关的
Contacts\u事件