我有一个协会,我想得到:
Person > has_many :animals > has_many (through animals) :dogs
> belongs_to :leash > belongs_to :collar
我想获得所有可能的:collar
选项,以便我可以检查该特定Person
是否有collar_id
要检查我喜欢的:leash
实例(person
是Person
的实例(:
person.dogs.where(leash_id: id_of_leash)
但是我现在需要更深入地了解是否在这个特定的人中找到了项圈 ID。
你可以试试这个:
person.dogs.include(leash: :collar(.where("collar.attribute = ?", condition(
我不确定这是将查询与迭代器混合的好习惯,但我发现这可以工作并且清楚地阅读:
person.dogs.to_a.detect{ |d| d.leash.collar.id == collar_id }