【发布时间】:2011-06-03 10:22:06
【问题描述】:
我正在尝试从我的数据库中检索一个 activerecord 对象。我的模型是
class User < ActiveRecord::Base
belongs_to :account
has_many :domains, :through => :account
end
和
class Account < ActiveRecord::Base
has_many :domains
has_many :users
end
和
class Domain < ActiveRecord::Base
belongs_to :account
end
现在我想根据用户名和域名检索用户(假设这些分别是 User 和 Domain 类的属性)。即类似于
User.find(:first, :conditions =>{:username => "Paul", :domains => { :name => "pauls-domain"}})
我知道上面的代码行不通,因为我不得不提一些关于 domains 表的内容。此外,用户和域之间的关联是一对多的(这可能会使事情进一步复杂化)。
关于如何形成此查询的任何想法?
【问题讨论】:
-
您确定您的模型关联正确吗?他们似乎没有。
标签: ruby-on-rails activerecord has-many-through