小编Ant*_*ine的帖子

查找与关联模型(表)至少具有一个关联的所有记录

假设我有这个:

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至少具有一个FlowerFruit实例或两者的所有实例?这个想法不是得到那些根本Tree没有的东西。FlowerFruit

model ruby-on-rails rails-activerecord

1
推荐指数
1
解决办法
2153
查看次数

标签 统计

model ×1

rails-activerecord ×1

ruby-on-rails ×1