acacia
acacia

Reputation: 1387

Trouble with model query

I am trying to make a search with custom data in this action here below,

def number_price(user_id,to)
    user = User.find(user_id)   
    prices = Price.where(user_id: user_id)
    price.each do |price|
      if to =~ /^(price.prefix)/
        return price."price_#{user_currency.downcase}"
      else
        return DefaultPrices."price_#{user_currency.downcase}"
      end    
end

But i having an error with this request here on the line;

return price."price_#{user_currency.downcase}"

Any idea how i can improve this and make it work.. i have a feeling it something silly.. Thank you

Upvotes: 0

Views: 38

Answers (2)

Roman Kiselenko
Roman Kiselenko

Reputation: 44360

Use public_send instead send because public_send invoke only public method on class instance:

class Foo
private
  def bar
    puts "Hi!"
  end
end
=> nil
=> f = Foo.new
=> #<Foo:0x007f83e3813af8>
=> f.bar
=> #NoMethodError: private method `bar' called for #<Foo:0x007f83e3813af8>
=> f.send(:bar)
Hi!
=> nil
class Baz
 def bor
   puts "Ho!"
  end
end
=> nil
=> s = Baz.new
=> #<Baz:0x007f83e2429da8>
=> s.bor
Ho!
=> nil
=> s.public_send(:bor)
Ho!
=> nil

Upvotes: 1

Siva
Siva

Reputation: 8058

I'm not sure how your model looks like

But I guess you are trying to achieve dynamic function call which can be done by

return price.send("price_#{user_currency.downcase}")

Or

return eval "price.price_#{user_currency.downcase}" 

Upvotes: 1

Related Questions