In my controller I am retrieving all quotes for a user like so:
# get all logged in person's quotes
@quotes = Quote.all(
:select => '*',
:joins => "LEFT JOIN statuses on statuses.id = quotes.status_id LEFT JOIN organisations on organisations.id = quotes.customer_id",
:conditions => ['raised_by = ?', @person.id],
:order => 'quotes.created_at ASC'
)
Which I then show in my view:
<% @quotes.each do |quote| %>
<tr>
<td><%= quote.name %></td>
<td><%= quote.partner_ref %></td>
<td><%= quote.status_name %></td>
<td><a href="/quotes/view/<%= quote.id %>">View</a></td>
</tr>
<% end %>
The problem is the <%= quote.id %>
used in the link is returning the id
column of the joined Organisations
table. I had the same problem with the name
column of the statuses
table since Organisations
also has a name
field but I just changed the name of the column but that's not something I can do to the Organisations
table since it's used extensively elsewhere.
So, is there someway to access the column names using some kind of alias?
Silly question by me.
Query needed was
SELECT organisations.name AS organisation_name, quotes.id, quotes.partner_ref, quotes.order_total, statuses.name AS status_name
FROM quotes
LEFT JOIN statuses on statuses.id = quotes.status_id
LEFT JOIN organisations on organisations.id = quotes.customer_id
WHERE raised_by = 'xx'
ORDER BY quotes.created_at ASC
Whereas before the query was simply
SELECT * FROM quotes
LEFT JOIN statuses on statuses.id = quotes.status_id
LEFT JOIN organisations on organisations.id = quotes.customer_id
WHERE raised_by = '940'
ORDER BY quotes.created_at ASC
So, the corresponding Rails code is
@quotes = Quote.all(
:select => 'organisations.name AS organisation_name, quotes.id, quotes.partner_ref, quotes.order_total, statuses.name AS status_name',
:joins => "LEFT JOIN statuses on statuses.id = quotes.status_id LEFT JOIN organisations on organisations.id = quotes.customer_id",
:conditions => ['raised_by = ?', @person.id],
:order => 'quotes.created_at ASC'
)