I had a terrible morning. Lots of emails floating around about why things don't work. Upon investigating I found that there is a data mismatch which is causing errors.
Scenario
Customer
and Address
are two tables.
Customer
contains
class Customer < ActiveRecord::Base
has_one :address, :foreign_key => "id"
end
Address
Contains
class Address < ActiveRecord::Base
belongs_to :customer, :foreign_key => "cid"
end
So the two tables match on id
which is the default and that column is auto incremented.
Problem on the edit Page we have some code like this.
params[:line1] = @customer.first.address.line1
It fails because no matching record is found for a customer in the address table. I don't know why this is happening. It seems that over time a lot of records did not get added to Address
table. Now problem is that when a new Customer
is added (say with id 500) the Address
will be added with some other id (say 425) ...now you don't know which address belongs to which customer.
Question
Being new to Rails, I am asking whether it is always considered good to create an extra column for joining of the records, rather than depending on the column that is automatically incremented? If I had a seperate column in Address
table where I would manually insert the recently added customers id then this issue would not have come up.