Hi,
I'm trying to run a query that returns columns from different tables
and that joins a couple of tables. Here it is
@result = EcOrder.find_by_sql["select
u1.ship_to_first_name, u1.ship_to_last_name, u1.ship_to_street,
u1.ship_to_state, u1.ship_to_zip, u1.ship_to_city, u1.ship_to_country,
u1.phone, u1.work_phone, u1.work_phone_extension, u1.email, u2.email,
u2.fax, u1.fax from ec_orders o, users u1, users u2 where o.user_id =
u1.id and u1.user_id = u2.id and o.id = ?", ec_order_id]
I didn't know what model to use, since I'm joining a couple. Anyway
the above produces the error:
ArgumentError in OrderController#submit
wrong number of arguments (0 for 1)
Any clarifications are appreciated, - Dave
You need to put parentheses around your argument.
@result = EcOrder.find_by_sql(["select u1.ship_to_first_name,
u1.ship_to_last_name, u1.ship_to_street, u1.ship_to_state,
u1.ship_to_zip, u1.ship_to_city, u1.ship_to_country, u1.phone,
u1.work_phone, u1.work_phone_extension, u1.email, u2.email, u2.fax,
u1.fax from ec_orders o, users u1, users u2 where o.user_id = u1.id
and u1.user_id = u2.id and o.id = ?", ec_order_id])
...should do.
Thanks, Matt. I have a follow-up question. When I try and access a
column from the result set
@result = EcOrder.find_by_sql(["select
u1.ship_to_first_name, u1.ship_to_last_name, u1.ship_to_street,
u1.ship_to_state, u1.ship_to_zip, u1.ship_to_city, u1.ship_to_country,
u1.phone, u1.work_phone, u1.work_phone_extension, u1.email, u2.email,
u2.fax from ec_orders o, users u1, users u2 where o.user_id = u1.id
and u1.user_id = u2.id and o.id = ?", ec_order_id])
@fax = @result.fax
I get the error
NoMethodError in OrderController#submit
undefined method `fax' for :Array
How do I get the column "fax" out of my result set? - Dave
@result is an array. You can either do @result.first.fax or iterate over the whole thing with
@result.each do |r|
do something with r.fax
end
Peace,
Phillip