I''m new to ruby and rails and as a result am a bit lost on implementing
a one to many relationship. In my DB I have 2 tables contents and
locations, contents contains the foreign key location_id. I generated a
content and a location model using rails scripts and then edited the
models as follows
class Content < ActiveRecord::Base
has_one :location
end
class Location < ActiveRecord::Base
belongs_to :content
end
When I attempt to access content.location I get this error
|RuntimeError: ERROR C42703 Mcolumn locations.content_id does not exist
Fparse_func.c
L1036 Runknown_attribute: SELECT * FROM locations WHERE (locations.content_id =
9) LIMIT 1|
I have been able to work around this by defining the association in the
content class as
has_many :location, :finder_sql => ''SELECT * FROM locations WHERE
locations.id = #{location_id}''
I attempted defining this association as has_one, but I am then not able
to overwrite finder_sql. I am hoping someone can explain a few things
to me.
Firstly, why does has_one expect locations to contain the content_id,
since it would require improper modeling in the DB?
Secondly, how do I properly associate the objects in Rails and overwrite
the location find method?
Thanks,