evamedia
(evamedia)
November 15, 2006, 5:00pm
1
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:
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
>
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 Paul
gustav@rails.co.za
rab
(Rob Biedenharn)
November 15, 2006, 9:56pm
3
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 }
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.
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:
<table>
<% for car in @cars %>
<td>
<%= car.name %>
</td>
<td>
<%= link_to 'show', :controller =>'newowner', :action => 'show',
:id => car.owner.id %>
</td>
</table>
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 Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
evamedia
(evamedia)
November 16, 2006, 9:51am
4
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