Topic: accessing corect ID

Been playing with RoR for a few weeks, got quite far, but having a stupid moment, and a morning of googling isn't getting me anywhere

in my controller, amongst others I have

@car_owners = CarOwner.find(:all, :joins => ["LEFT JOIN users ON = user_id"], :conditions => ["car_ID = ?",])

in the related view I have

<% for owner in @car_owners %>

.. snip ..

<%= link_to 'show', :controller =>'newowner', :action => 'show', :id => %>

<% end %>

The problem is the is actually giving me the as in the left join in the controller

How do I access the actual , I've tried various things, but nothing is giving me the right id

thanks in advance for any help

Re: accessing corect ID

If you have the CarOwner and User models set up correctly with their association (has_many/belongs_to) you should be able to do this:

@car_owners = CarOwner.find(:all, :included => :users, :conditions => ["car_ID = ?",])

Better yet, if I'm understanding the associations correctly and you have a has_many :through set up, you could do this:

@car_owners = @car.car_owners

If you give me more information on how the models relate to one another, I can provide some code for doing this has_many :through association.

Last edited by ryanb (2006-11-15 14:26:00)

Railscasts - Free Ruby on Rails Screencasts

Re: accessing corect ID

ryanb, thanks for the reply


  has_many :car_owners


  belongs_to :car
  has_one :user

as each car could of been owned by many people, but each owner is only one user

(hope that makes sense)

with my code, looking at the log it's running a query "select * from car_owners left join users on = user_id where (car_id = 9)

if I run that on my DB I get all the rows (*) so how do I access

Re: accessing corect ID

I'm still not certain how the association is. Which table has the user_id column in it?

Also, if you  have a @car instance variable, and you're trying to fetch the car_owners for that car, you just have to do this:


There's no need to do a special find and join the users table because the car and car_owners tables are directly joined.

Railscasts - Free Ruby on Rails Screencasts

Re: accessing corect ID

oooo...that could be it

so, would I be able to access the car_owners id by


Re: accessing corect ID

Nope @car.car_owners returns an array, so you need to loop through it:

<% for owner in @car.car_owners %>
  <%= %>
<% end %>

Railscasts - Free Ruby on Rails Screencasts

Re: accessing corect ID

thanks ryanb, that's something else I've learnt , but this now causes another problem, which is why I did the left join in the first place

car_owners has a user_id

with my left join, I was joining the users table on this user_id so I had access to the fields in that

With the above, I no longer have that

is this where the :through you mentioned comes into play

Re: accessing corect ID

You can access the user for a particular owner like this:
# etc...

Railscasts - Free Ruby on Rails Screencasts

Re: accessing corect ID

thanks for the help, but that just gives me

Unknown column 'users.car_owner_id' in 'where clause': SELECT * FROM users WHERE (users.car_owner_id = 27)

car_owners has a user_id , but users doesn't have a car_owner_id

Re: accessing corect ID

actually, think I may have solved it

the belongs_to and has_one seems to work the wrong way round to how I was expecting it

car_owner - belongs_to :user

user -> has_one :car_owner

sure this will bite me in the a$$ later on in my app, I'll cross that bridge when I come to it

thanks for bearing with me

Re: accessing corect ID

Think of it this way: belongs_to is always used in the table that has the foreign key. The has_many/has_one one is always used in the table that doesn't have the foreign key.

Railscasts - Free Ruby on Rails Screencasts