I think your original question was, how do I get the the cdetails's
company_name, given a position. Why can't you just do:
position.cdetail.company_name
?
No, that is just giving me "undefined method `cdetail' for
#<Position:0x4c56748>"
"belongs_to :cdetail" adds an accessor method to Position which results
in the Cdetail referenced by the cdetail_id field in Position (you can
change the name of the field with :foreign_key, if your field is named
something other than cdetail_id).
Well , I've been trying this and it seems to have no effect -
continuing to get undefined method - "cdetail"
Here is the relationship defined in Position model
belongs_to :cdetail, :foreign_key => :company_id
So, in the Position model , the attribute is company_id
and in the Cdetail model, the attribute is company_id , perhaps having
both columns with the same name is confusing the issue.
if you wanted to access users directly from cdetail (use :foreign_key
and :source if the field names are weird).
I haven't gotten a good feel for the :source option. Reading through
the doc it seems to reference another model ?
Tangent: Is there a reason you're using an unintuitive reference like
"cdetail"? Why not just call it a "company", with fields "id" and
"name"? Or even "company_detail"?
Because I didn't think it through and I'm being hard headed
Actually company_detail maybe a better idea, if that would truly help
the association problem. The c in cdetail obviously stands for
company. Just in case it seemed my model naming is totally
irrelevant.
For the time being I solved the issue by adding the :join option to
the find. It's not totally ugly and seems to give me the results.
However I'd like to do it, as said above
position.cdetail.company_name.
Stuart