I am trying to list my orders showing customer names rather than
customer IDs. I am not having much luck.
I have a table called orders, having a ‘customer_id’ field. I also have
a table called customers, which has a field called ‘id’, as well as a
field called ‘name’.
In the orders model, I specify that order belongs_to customer, and in
the customer model, I specify that the customer has_many orders.
As a first shot, I assumed that in the list view for orders, I could
replace the working line
<td><%=h order.send('customer_id') %></td>
with something like
<td><%=h order.send('order.customer.name') %></td>.
That didn’t work.
My current iteration defines the following method in order.rb
def self.find_all_denormalized
sql = " SELECT orders.*, customers.name as
customerName
From orders left join customers
on customer_id = customers.id
"
orders_denormalized = Order.find_by_sql(sql)
return orders_denormalized
end
and in order_controller.rdb, I have redefined list as
def list
@order_pages, @orders = paginate Order.find_all_denormalized,
:per_page => 10
end
This is not working. I am getting a message like:
wrong constant name
#Order:0x394ba68#Order:0x394ba20#Order:0x394b9d8#Order:0x394b990#Order:0x394b948
Two questions: why is my approach not working, and is there a
fundamentally better (more Rails) way to do this?
Thanks in advance
Ed