I am reading the docs, and they say about belongs_to:
Specifies a one-to-one association with another class. This method should only be used if this class contains the foreign key.
I have 3 models:
Info
Customer
Seller
I need that every instance of Customer or Seller can be linked to one row in the infos table.
So I think I just need to put the foreing_key info_id in the 2 models, and specify a belongs_to :info
association.
But when I do this:
@customer.info
It gives me error because is trying to execute this query:
SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`customer_id` = 1 LIMIT 1
But for what I need and what I underatand from the docs it should execute this one:
SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`id` = 1 LIMIT 1
What am I missing?
You should do it like
info model
customer model
seller model
model having belongs_to always contain foreign key. So customer and seller table will have info_id foreign keys.
After that you can get both customer and seller linked with info and vice versa.