I was a little addicted to changing foreign keys by associating tables with each other.
products |
---|
id |
number |
price |
features |
---|
id |
product_number |
etc... |
I want to associate the number of products with the product_number of features.
has_one :feature, foreign_key: "number", primary_key: "product_number"
belongs_to :product, foreign_key: "product_number", primary_key: "number"
In other words, when the feature is referenced from the product side, records with the same product number and feature product_number are linked. And vice versa.
foreign_key Specify a foreign key(Specify the column of the referencing table).. The default is hogehoge_id。
primary_key Column used when referencing another table(Specify the column of the referenced table)Is specified. Id by default.
Recommended Posts