我有一个has_many与另一个模型有关系的模型,如下所示:
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
Run Code Online (Sandbox Code Playgroud)
由于有些父母可能没有孩子,我想做一个只返回有孩子的父母的查询.我怎么会这样做?
由于我在Rails 3上执行此操作,如果此查询不使用where.not.语法将会有所帮助.
假设我有这个:
class Tree < ActiveRecord::Base
has_many :fruits
has_many :flowers
end
class Fruit < ActiveRecord::Base
belongs_to :tree
end
class Flower < ActiveRecord::Base
belongs_to :tree
end
Run Code Online (Sandbox Code Playgroud)
如何进行有效的查询来获取Tree至少具有一个Flower或Fruit实例或两者的所有实例?这个想法不是得到那些根本Tree没有的东西。FlowerFruit