sql - RAILS: has_one association based on non PK column -
i have following tables:
users [id | name | type] types [id | title]
i need setup has_one
association ('users'.type => 'types'.id). but, understand, when write like:
user.rb
has_one :type
it seems create relation between 'users'.id , 'types'.id. how specify 'users'.type => 'types'.id has_one association?
finally found solution:
user.rb
belongs_to :type
type.rb
has_many :users, foreign_key: "type_id"
Comments
Post a Comment