Using :through

Just do (in shorthand):

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).

On 9/25/06, ASE [email protected] wrote:

Forget what I said before. I would set up a has_one/belongs_to

Got to run out , but will work on this in a few hours.
Let you know how things work out a little later.

Stuart

On 9/26/06, ASE [email protected] wrote:

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.

Stuart

bump :slight_smile:

Stuart

On 9/26/06, Dark A. [email protected] wrote:

can’t do habtm or hmt. Make sure the fields which hold the referenced
id : auto_incrementing
position.company.name

Maybe it makes sense to go through User but truthfully it’s not giving
me any better results.

Stuart

Stuart Fellowes wrote:

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:

def company
user.company
end

On 9/26/06, Dark A. [email protected] wrote:

On 9/26/06, Ashley T. [email protected] wrote:

I know by now ya think I’m crazy but I put it together exactly how
recommended. I’m still getting an undefined method for company.

class Company < ActiveRecord::Base
belongs_to :user
end

class User < ActiveRecord::Base
has_one :company
end

class Position < ActiveRecord::Base

belongs_to :user
end

companies table:
| id | user_id | name |

users table:

| id | company_id | # I also tried changing this to user_id
(rebooted server) no dice

I added this to the position.rb controller:
def company
user.company
end

Calling it through:
position.company.name %>

Stuart

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 ?

Stuart

On 9/26/06, Dark A. [email protected] wrote:

class User < ActiveRecord::Base

Calling it through:
position.company.name %>

Stuart

On 9/26/06, Ashley T. [email protected] wrote:

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 ?

Stuart