# Wrong number of arguments to find\_by\_sql?

**URL:** <https://www.ruby-forum.com/t/wrong-number-of-arguments-to-find-by-sql/129245>\
**Category:** Rails\
**Created:** [February 15, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-to-find-by-sql/129245 "2008-02-15T19:01:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 15, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-to-find-by-sql/129245/1 "2008-02-15T19:01:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 15, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-to-find-by-sql/129245/2 "2008-02-15T19:42:47Z")

</div>

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.

On Feb 15, 11:00 am, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-to-find-by-sql/129245/3 "2008-02-16T22:17:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-to-find-by-sql/129245/4 "2008-02-16T23:32:05Z")

</div>

On Feb 16, 2008, at 3:16 PM, removed\_email\_address@domain.invalid wrote:

> ```
> @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
