Possible to alias a belongs_to association in Rails?

前端 未结 4 1645
情书的邮戳
情书的邮戳 2020-12-25 10:18

I have a model with a belongs_to association:

class Car < ActiveRecord::Base
  belongs_to :vendor
end

So I can call c

4条回答
  •  無奈伤痛
    2020-12-25 10:25

    No it doesn't look for company_id for instance change your code as follows

    In Rails3

    class Car < ActiveRecord::Base
       belongs_to :vendor
       belongs_to :company, :class_name => :Vendor,:foreign_key => "vendor_id"
    end
    

    In Rails4

    We can use alias attribute.

    alias_attribute :company, :vendor
    

提交回复
热议问题