Rails has_one 通过集合
我将首先粘贴相关代码,然后稍后解释我想要做什么
class User < ActiveRecord::Base
has_many :compus, :dependent => :destroy
has_many :companies, :through => :compus
end
class Company < ActiveRecord::Base
has_many :compus, :dependent => :destroy
has_many :employees, :through => :compus, :source => :user
has_one :owner, :through => :compus, :source => :user, :conditions => { :owner => true }
end
class Compu < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
compus 表具有以下列(从迁移中复制)
create_table :compus do |t|
t.references :company
t.references :user
t.string :job_title
t.boolean :owner, null: false, default: false
t.timestamps
end
因此,如您所见,我有一个可以创建 n 个公司的用户并且在n家公司有工作,在一家公司有工作并不意味着他是所有者..
我想要得到的是:
user.companies #=>用户创建的公司或在其中工作的公司(我担心稍后会过滤结果)
company.employees #=>所有在该公司工作的用户(通过 compus)
company.owner #=>最初创建公司的一个用户(如果稍后转移,则为另一个用户)
所以我在编写上面的代码之前添加了以下规范。
it "has one owner" do
company = Factory(:company)
user = Factory(:user)
company.should respond_to(:owner)
user.companies << company
company.owner.should === user
end
但我收到以下错误:
ActiveRecord::HasOneThroughCantAssociateThroughCollection:
Cannot have a has_one :through association 'Company#owner' where the :through association 'Company#compus' is a collection. Specify a has_one or belongs_to association in the :through option instead.
那么如何在不向公司表添加更多列的情况下解决此问题,如果我在公司表上添加 owner_id 会更容易,但这会导致重复数据库,通常如果用户创建了一家公司,这意味着他在该公司工作
我在其中的另一个问题是,如何通过关联轻松访问 compus 表上的 :job_title ?
I'll start by pasting the relevant code and then I'll explain later what I am trying to do
class User < ActiveRecord::Base
has_many :compus, :dependent => :destroy
has_many :companies, :through => :compus
end
class Company < ActiveRecord::Base
has_many :compus, :dependent => :destroy
has_many :employees, :through => :compus, :source => :user
has_one :owner, :through => :compus, :source => :user, :conditions => { :owner => true }
end
class Compu < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
The compus table has the following columns (copied from the migration)
create_table :compus do |t|
t.references :company
t.references :user
t.string :job_title
t.boolean :owner, null: false, default: false
t.timestamps
end
So as you can see, I have a user who can create n companies and have jobs at n companies, having a job at a company does not mean he's the owner..
What I would like to get is:
user.companies #=> The companies the user has created, or have jobs at (I worry about filtering results later)
company.employees #=> All the users that have jobs at this company (through compus)
company.owner #=> The one user that has originally created the company (or another one if transferred later)
So I added the following spec before I wrote the code above.
it "has one owner" do
company = Factory(:company)
user = Factory(:user)
company.should respond_to(:owner)
user.companies << company
company.owner.should === user
end
but I'm getting the following error:
ActiveRecord::HasOneThroughCantAssociateThroughCollection:
Cannot have a has_one :through association 'Company#owner' where the :through association 'Company#compus' is a collection. Specify a has_one or belongs_to association in the :through option instead.
So how can resolve this problem without adding more columns to the companies table, it would be easier if I added owner_id on the companies table but that would lead to duplications in the database, usually if a user created a company it means that he works at it
Another question while I'm at it, How can I easily access the :job_title that is on the compus table through the association?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个函数来返回所有者
就数据库设计而言,如果您想将一家公司限制为一个所有者,那么最好在公司表中添加一个owner_id 列。否则你必须强制他们在任何时候都是一个所有者,而且事情会更加复杂。
You can just make a function to return the owner
In terms of database design, if you want to limit a company to one owner it would be a good idea to put an owner_id column in the company table. Otherwise you have to enforce that their is exactly one owner at all times, and it will be more complicated.