Undefined method for array

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?

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.
[email protected]
wrote:

4: :select => “base_amount, special_amount”,

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs