Ruby-on-rails 可以在 Rails 中给关联关联别名吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21471075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible to alias a belongs_to association in Rails?
提问by at.
I have a model with a belongs_toassociation:
我有一个带有belongs_to关联的模型:
class Car < ActiveRecord::Base
belongs_to :vendor
end
So I can call car.vendor. But I also want to call car.company! So, I have the following:
所以我可以打电话car.vendor。但我也想打电话car.company!所以,我有以下几点:
class Car < ActiveRecord::Base
belongs_to :vendor
def company
vendor
end
end
but that doesn't solve the assignment situation car.company = 'ford', so I need to create another method for that. Is there a simple aliasmechanism I can use for associations? Can I just use alias_method :company, :vendorand alias_method :company=, :vendor=?
但这并不能解决分配情况car.company = 'ford',所以我需要为此创建另一种方法。是否有一种简单的alias机制可以用于关联?我可以只使用alias_method :company, :vendorandalias_method :company=, :vendor=吗?
回答by pramod
No it doesn't look for company_id for instance change your code as follows
不,它不会查找 company_id 例如更改您的代码如下
In Rails3
在 Rails3 中
class Car < ActiveRecord::Base
belongs_to :vendor
belongs_to :company, :class_name => :Vendor,:foreign_key => "vendor_id"
end
In Rails4
在 Rails4 中
We can use alias attribute.
我们可以使用别名属性。
alias_attribute :company, :vendor
回答by Fran Worley
In Rails 4, you should simply be able to add alias_attribute :company, :vendorto your model.
在 Rails 4 中,您应该能够简单地添加alias_attribute :company, :vendor到您的模型中。
回答by Vishal S Mujumdar
Short Version:
精简版:
Generate model with migration
$ rails generate model Car vendor:references name:string ...Add following line in
Carmodel i.ecar.rbfileclass Car < ActiveRecord::Base belongs_to :company, :class_name => 'Vendor', :foreign_key => 'vendor_id' endNow you have
@car.companyinstance method.
使用迁移生成模型
$ rails generate model Car vendor:references name:string ...在
Car模型 iecar.rb文件中添加以下行class Car < ActiveRecord::Base belongs_to :company, :class_name => 'Vendor', :foreign_key => 'vendor_id' end现在你有了
@car.company实例方法。
For a Detailed explanation read ahead [Optional if you understood the above !!]
有关详细说明,请提前阅读[如果您理解上述内容,则可选!!]
Detailed Version:
详细版本:
The model Carwill have an association with the model Vendor(which is obvious). So there should be a vendor_idin the table cars.
模型Car将与模型有关联Vendor(这是显而易见的)。所以vendor_id表中应该有一个cars。
In order to make sure that the field
vendor_idis present in thecarstable run the following on the command line. This will generate the right migration. Thevendor:referencesis important. You can have any number of attributes after that.$ rails generate model Car vendor:references name:stringOr else in the existing migration for
create_table :carsjust add the linet.references :vendorclass CreateCars < ActiveRecord::Migration def change create_table :cars do |t| t.string :name ... t.references :vendor t.timestamps end end endThe final thing that you need to do is edit the model
Car. So add this code to yourcar.rbfileclass Car < ActiveRecord::Base belongs_to :company, :class_name => 'Vendor', :foreign_key => 'vendor_id' end
为了确保该字段
vendor_id存在于cars表中,请在命令行上运行以下命令。这将生成正确的迁移。该vendor:references是很重要的。之后您可以拥有任意数量的属性。$ rails generate model Car vendor:references name:string或者在现有迁移中
create_table :cars添加行t.references :vendorclass CreateCars < ActiveRecord::Migration def change create_table :cars do |t| t.string :name ... t.references :vendor t.timestamps end end end您需要做的最后一件事是编辑模型
Car。所以将此代码添加到您的car.rb文件中class Car < ActiveRecord::Base belongs_to :company, :class_name => 'Vendor', :foreign_key => 'vendor_id' end
After you do the third step you will get the following instance methods for the model Carprovided by Rails Associations
做完第三步之后,你会得到以下CarRails Associations提供的模型的实例方法
@car.companyWhen you do
@car.companyit will return a#<Vendor ...>object. To find that#<Vendor ...>object it will go look for thevendor_idcolumn in thecarstable because you have mentioned:foreign_key => 'vendor_id'You can set the company for a car instance by writing
@car.company = @vendor || Vendor.find(params[:id]) #whichever Vendor object you want @car.saveThis will save the
idof thatVendorobject in thevendor_idfield of thecarstable.
@car.company当你这样做时,
@car.company它会返回一个#<Vendor ...>对象。要找到该#<Vendor ...>对象,它将查找表中的vendor_id列,cars因为您已经提到:foreign_key => 'vendor_id'您可以通过编写为汽车实例设置公司
@car.company = @vendor || Vendor.find(params[:id]) #whichever Vendor object you want @car.save这将节省的
id那的Vendor对象在vendor_id该领域cars表。
Thank You.
谢谢你。
回答by nickcen
class Car < ActiveRecord::Base
belongs_to :vendor
belongs_to :company, :class_name => :Vendor
end

