# Accessing correct ID

**URL:** <https://www.ruby-forum.com/t/accessing-correct-id/76068>\
**Category:** Rails\
**Created:** [November 15, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068 "2006-11-15T18:02:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![evamedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@evamedia](https://www.ruby-forum.com/u/evamedia)\
**Post date:** [November 15, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068/1 "2006-11-15T18:02:52Z")

</div>

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  
users.id = user\_id”], :conditions =\> [“car\_ID = ?”, @car.id])

in the related view I have

\<% for owner in @car\_owners %\>

… snip …

\<%= link\_to ‘show’, :controller =\>‘newowner’, :action =\> ‘show’, :id =\>  
owner.id %\>

\<% end %\>

The problem is the owner.id is actually giving me the users.id as in  
the left join in the controller

How do I access the actual car\_owners.id , I’ve tried various things,  
but nothing is giving me the right id

thanks in advance for any help

---

<div class="post-metadata">

**Author:** ![evamedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@evamedia](https://www.ruby-forum.com/u/evamedia)\
**Post date:** [November 15, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068/2 "2006-11-15T18:18:54Z")

</div>

evamedia wrote:

> \<% for owner in @car\_owners %\>
> 
> How do I access the actual car\_owners.id , I’ve tried various things,  
> but nothing is giving me the right id
> 
> thanks in advance for any help
> 
> >

You could try adding

:select =\> “car\_owners.id as car\_owner\_id”

And then saying link\_to … :id =\> owner.car\_owner\_id

It sounds like you should be using belongs\_to, as in

@car\_owners = CarOwner.find(:all, :include =\> :user, :conditions =\>  
[“car\_ID = ?”, @car.id])

Just guessing…

Hope this helps (not to mention works :] )…

Cheery-o  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![evamedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@evamedia](https://www.ruby-forum.com/u/evamedia)\
**Post date:** [November 15, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068/3 "2006-11-15T22:36:06Z")

</div>

You need to set up your classes:

Car  
has\_one :owner

Owner  
belongs\_to :car

Then grab car and owner info:

@cars = Car.find(:all)  
@cars.collect { |car| car.owner }

You will now have a collection of all cars with owners nested beneath  
each car. Use \<%= debug(@cars) %\> in your view to verify that this has  
happened. To show in the view iterate though cars and owners:

 \<% for car in @cars %\> 

| \<%= car.name %\> | \<%= link\_to 'show', :controller =\>'newowner', :action =\> 'show', :id =\> car.owner.id %\> |

Things may get a bit funky but this should get you close enough,  
assuming only one owner per car. Hope this helps some!

Taylor

---

<div class="post-metadata">

**Author:** ![evamedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@evamedia](https://www.ruby-forum.com/u/evamedia)\
**Post date:** [November 15, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068/4 "2006-11-15T23:22:00Z")

</div>

On Nov 15, 2006, at 4:36 PM, Taylor S. wrote:

> Then grab car and owner info:
> 
> @cars = Car.find(:all)  
> @cars.collect { |car| car.owner }

I think this was meant to be:  
@cars = Car.find(:all, :include =\> :owner)

which will be a reduction in database overhead by avoiding separate  
queries for the owner of each car.

> \<%= link\_to 'show', :controller =\>'newowner', :action =\> 'show', :id =\> car.owner.id %\> 
> 
> Things may get a bit funky but this should get you close enough,  
> assuming only one owner per car. Hope this helps some!
> 
> Taylor

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![evamedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@evamedia](https://www.ruby-forum.com/u/evamedia)\
**Post date:** [November 16, 2006, 11:14am UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068/5 "2006-11-16T11:14:59Z")

</div>

It seems to me that owner = user? What about this schema?

Car  
has\_many :users, :through =\> :ownerships, :conditions =\> “ended\_at IS  
NULL”

User  
has\_many :cars, :through =\> :ownerships, :conditions =\> “ended\_at IS  
NULL”

Ownership  
belongs\_to :car  
belongs\_to :user

Now you can do Car(id).users[0].id to get the current owner’s id. There  
is some trickery involved because you aren’t really using a join table  
so you identify the current owner as the only one who has a NULL  
“ended\_at” ownership. (when a car is sold, ended\_at is filled in)

But since Rails cannot do a has\_one through a M:M table you have to  
create an array. ‘:conditions =\> “ended\_at IS NULL”’ insures that your  
array only has one record, so you can reference it with ‘users[0].’  
Does that make sense?

---

<div class="post-metadata">

**Author:** ![evamedia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@evamedia](https://www.ruby-forum.com/u/evamedia)\
**Post date:** [November 16, 2006, 10:55am UTC](https://www.ruby-forum.com/t/accessing-correct-id/76068/6 "2006-11-16T10:55:13Z")

</div>

thanks for the replys, but my models are setup as

Car

has\_many :car\_owners

CarOwner

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 users.id = user\_id where (car\_id = 9)

if I run that on my DB I get all the rows (\*) so how do I access  
car\_owners.id
