[英]Rails has_many :through and has_one :through associations
First I'm using Rails 3.1 from the 3-1-stable branch updated an hour ago. 首先,我使用一个小时前更新的3-1-stable分支中的Rails 3.1 。
I'm developing an application where I have 3 essential models User , Company and Job , Here's the relevant part of the models: 我正在开发一个应用程序,其中有3个基本模型User , Company和Job ,这是模型的相关部分:
class User < ActiveRecord::Base
has_many :companies_users, class_name: "CompaniesUsers"
has_many :companies, :through => :companies_users, :source => :company
end
class Company < ActiveRecord::Base
has_many :companies_users, class_name: "CompaniesUsers"
has_many :employees, :through => :companies_users, :source => :user
has_many :jobs, :dependent => :destroy
end
class Job < ActiveRecord::Base
belongs_to :company, :counter_cache => true
end
class CompaniesUsers < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
The code works just fine, but I have been wondering if it's possible to: 该代码工作正常,但我一直在想是否可以:
I want to link a job with an employer , so think of this scenario: A user John who's an employee at Example , he posted the job Rails Developer, so I want to access @job.employer and it should get me back the user John , in other words: 我想将工作与雇主联系起来 ,因此请考虑以下情形:作为Example的雇员的John用户,他发布了Rails Developer的工作,因此我想访问@ job.employer ,它应该使我回到用户John的位置。 , 换一种说法:
@user = User.find_by_name('john')
@job = Job.find(1)
@job.employer == @user #=> true
So I thought of two possible solutions 所以我想到了两种可能的解决方案
class Job
has_one :employer, :through => :employers
end
class User
has_many :jobs, :through => :employers
end
class Employer
belongs_to :job
belongs_to :user
end
class Job
has_one :employer, :class_name => "User"
end
class User
belongs_to :job
end
Which route should I go? 我应该走哪条路线? Is my code right ? 我的代码对吗?
I have another question, how to get rid of the class_name => "CompaniesUsers" option passed to has_many, should the class be Singular or Plural ? 我还有另一个问题,如何摆脱传递给has_many的class_name =>“ CompaniesUsers”选项,该类应该是Singular还是Plural? Should I rename it to something like Employees ? 我应该将其重命名为雇员吗?
PS: I posted the same question to Ruby on Rails: Talk PS:我向Ruby on Rails发表了同样的问题:Talk
Unless I'm missing something, I'd suggest simply doing 除非我缺少任何东西,否则我建议您简单地做
class Job
belongs_to :employer, :class_name => "User"
end
class User
has_many :jobs
end
This would give you methods like 这将为您提供类似的方法
user = User.first
user.jobs.create(params)
user.jobs # array
job = user.jobs.first
job.employer == user # true
You'll need an employer_id
integer field in your Jobs table for this to work. 为此,您需要在Jobs表中添加一个employer_id
整数字段。
Typically you want to name your pass through model: 通常,您要命名传递模型:
company_user
Then you don't need this: 然后,您不需要这个:
class_name: "CompaniesUsers"
Just make sure the name of your database table is: 只要确保您的数据库表的名称是:
company_users
What you have works for you, so that's great. 您所拥有的为您工作,所以很棒。 I just find when I don't follow convention I run in to trouble down the road. 我只是发现,如果我不遵守约定,就会遇到麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.