Accessing correct 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
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

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.
[email protected]

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

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
[email protected]

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs