class User; has_one :cdetail; has_many :positions; end
class Cdetail; belongs_to :user; end
class Position; belongs_to :user; end
User is the common connection between Position and Cdetail, but it is
not a join table (C and P point to U, not the other way around), so you
can’t do habtm or hmt. Make sure the fields which hold the referenced
ids are named properly or you tell Rails what they’re named in the
relationship definition (user_id, or :foreign_key => :company_id).
ids are named properly or you tell Rails what they’re named in the
relationship definition (user_id, or :foreign_key => :company_id).
I’m not sure I understand why I need to go through User to get what I
need out of Cdetail. Anyway, before explaining , let me try and
understand it first on my own.
However, as suggested I’ve made some changes, renaming Cdetail to
Company
Company has these relevant attributes:
id : auto_incrementing
company_id
name
Position has this relevant attribute:
company_id
So what I thought made sense here was to add association like this to
Position
belongs_to: company, :foreign_key => company_id
Then in show, list
position.company.name
Maybe it makes sense to go through User but truthfully it’s not giving
me any better results.
Again, company_id in both the positions table and the cdetails table
comes from the user id in the session information.
Stuart Fellowes wrote:
Company
company_id
Position
company_id
So what I thought made sense here was to add association like this to
Position
belongs_to: company, :foreign_key => company_id
I think your own naming conventions are confusing you (they’re confusing
me). As you said before, company_id holds the id of a record in the
users table (so please, please rename it user_id – you’ll be thankful
you did in three months). When you say “Position belongs_to :company, :
foreign_key => :company_id”, you’re telling Rails that company_id holds
an id of a record in the companies table, which is not the case.
Position and Company both “point to” User, in that they each have a
field which holds the id of a record in users. They have no relationship
with each other except indirectly through User (but not in a habtm or
hmt way). Once you’ve set up the basic relationships from C->U and P->U,
you can write a simple method in Position like this:
Since I tried everything suggested, read and googled with no end in
site - I came up with what I think is a viable solution, I made the
company id attribute not to auto increment but to use the company id
in that column. Seems to work , any possible problems ?
company_id
an id of a record in the companies table, which is not the case.
company_id DOES hold a record in the companies table.
companies:
| id | company_id | company_name | address | city | so on …
So why can I just connect these two tables , even though I realize the
id in the companies table is not the standard id as far as AR goes ?
Am I crazy and beating a dead horse or can this not be done. I am
under the impression there are ways to let AR know of these unusual
attribute names ?