# Undefined method for array

**URL:** <https://www.ruby-forum.com/t/undefined-method-for-array/126662>\
**Category:** Rails\
**Created:** [January 21, 2008, 6:51am UTC](https://www.ruby-forum.com/t/undefined-method-for-array/126662 "2008-01-21T06:51:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [January 21, 2008, 6:51am UTC](https://www.ruby-forum.com/t/undefined-method-for-array/126662/1 "2008-01-21T06:51:21Z")

</div>

I’m getting the following error:

undefined method `base\_amount’ for []:Array

It’s talking about line 6

1: def pay\_commission  
2: @product = Product.find(params[:id])  
3: pay = PayRate.find(:all,  
4: :select =\> “base\_amount, special\_amount”,  
5: :conditions =\> [“deleted\_yn=0 and pay\_type\_id = ?”,  
@product.pay\_type\_id])  
6: @product.amount = pay.base\_amount  
7: @product.special = pay.special\_amount  
8: @product.status = ‘Paid’  
9: end

If i take out lines 6 and 7, everything works fine. Also, when i look  
at the terminal session, the correct SQL is created and I can run that  
in MySQL to get the correct fields and values. Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [January 21, 2008, 7:24am UTC](https://www.ruby-forum.com/t/undefined-method-for-array/126662/2 "2008-01-21T07:24:06Z")

</div>

Is deleted\_yn is actually a boolean field if a pay rate has been deleted  
or  
not. You could call this deleted.

Then you could do:

has\_many :pay\_rates, :conditions =\> “deleted = 0”

in your Product model.

The reason why it’s giving you that error is because you’re retreving  
MANY  
payrates instead of just the one. Perhaps with this new code you can do  
@  
product.pay\_rates.first.amount

On Jan 21, 2008 4:21 PM, George P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## 4: :select =\> “base\_amount, special\_amount”,
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [January 21, 2008, 7:30am UTC](https://www.ruby-forum.com/t/undefined-method-for-array/126662/3 "2008-01-21T07:30:31Z")

</div>

Hi,  
pay = PayRate.find(:all,  
:select =\> “base\_amount, special\_amount”,  
:conditions =\> [“deleted\_yn=0 and pay\_type\_id = ?”,  
@product.pay\_type\_id])

You got the array of records from PayRate table because you find all  
records based on that conditions… So you have to use loop

```
 you can try this

 pay.each {|p| puts p.base_amount}

 If you use find :first your code is correct

 @product.amount = pay.base_amount
 @product.special = pay.special_amount

```

Regards  
Abirami

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [January 21, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/undefined-method-for-array/126662/4 "2008-01-21T21:14:53Z")

</div>

Thanks a lot. That fixed it

Abirami S. wrote:

> Hi,  
> pay = PayRate.find(:all,  
> :select =\> “base\_amount, special\_amount”,  
> :conditions =\> [“deleted\_yn=0 and pay\_type\_id = ?”,  
> @product.pay\_type\_id])
> 
> You got the array of records from PayRate table because you find all  
> records based on that conditions… So you have to use loop
> 
> ```
> you can try this
> 
> pay.each {|p| puts p.base_amount}
> 
> If you use find :first your code is correct
> 
> @product.amount = pay.base_amount
> @product.special = pay.special_amount
> 
> ```
> 
> Regards  
> Abirami
