Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.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.1 rails habtm与连接模型-如何查找未链接的记录?_Ruby On Rails 3.1_Has And Belongs To Many - Fatal编程技术网

Ruby on rails 3.1 rails habtm与连接模型-如何查找未链接的记录?

Ruby on rails 3.1 rails habtm与连接模型-如何查找未链接的记录?,ruby-on-rails-3.1,has-and-belongs-to-many,Ruby On Rails 3.1,Has And Belongs To Many,我使用表示连接的模型实现了habtm关系,因为连接具有以下属性: class Person has_many :person_photos end class PersonPhoto # has a person and a photo id, and a couple of # other attributes that aren't relevant end class Photo has_many :person_photos end 我想向Photo类添加一个方法

我使用表示连接的模型实现了habtm关系,因为连接具有以下属性:

class Person
  has_many :person_photos
end

class PersonPhoto
  # has a person and a photo id, and a couple of 
  # other attributes that aren't relevant
end

class Photo
  has_many :person_photos
end

我想向Photo类添加一个方法,以获取不在该照片中的人的列表。为了我的生命,我无法理解它。这容易吗

你可以试着让person通过person-u-photos和photo通过person-u-photos拥有很多人

Rails文档

然后photo.find1.people会列出照片1中的所有人

然后使用数组减法people\u not\u in\u photo=people.all-photo.find1。我想,人们会给你想要的东西


关于Stackoverflow的问题

尝试一下,也许在你的照片模型中

def people_not_tagged
    People.where("id NOT IN (?)", people_ids.empty? ? "" : people_ids)
end

people\u not\u in\u photo=people.all-photo.find1.people。照现在的样子,它会很重,对吗?也许people\u not\u in\u photo=people。选择:id.map&:id-photo.find1.people\u id会更好一些。但是,如果有很多用户,我认为这不是一个可行的解决方案。我喜欢这一个的简单性,但它生成的SQL是:从id不在1,2中的人中选择人。*这会让人感到恶心。啊-在括号中加括号?而且它有效!我编辑了答案来修正它。谢谢你在建什么?如果它不是一个非常小的个人应用程序,你会得到很多人!你可能想说得更具体一些。这是一个内部应用程序,所以不会有成千上万的人。实际上,这些模型甚至不是人物和照片,我只是使用了这些名称来让示例更容易理解。好的:但至少使用这种方法,如果需要,很容易分页。