我见过一些在 Rails 中:include
调用 ActiveRecord 方法时传递哈希值的例子。find
但是,我还没有看到任何关于这是否可以通过关系方法实现的示例。例如,假设我有以下内容:
def User < ActiveRecord::Base
has_many :user_favorites
has_many :favorites, :through => :user_favorites
end
def Favorite < ActiveRecord::Base
has_many :user_favorites
has_many :users, :through => :user_favorites
end
def UserFavorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorite
end
我看到的所有示例都显示如下代码:
User.find(:all, :include => :favorite)
但我没有看到任何显示使用关系的示例。我可以做这样的事情吗?
User.favorites(:include => :user)