views:

27

answers:

1

How do I pass a value to a dynamic method name that's called on a rails model?

@model.send(dynamic_method_name.to_sym,123)

This gives the error:

 wrong number of arguments (1 for 0)

If I were to use the same method like this though:

 @model.post_id = 123

then it works.

So there is a "setter" method for the method post_id (and therefore the dynamic method name).

How do I pass a value to this setter when the method is dynamic?

thanks!

+1  A: 

You need to add the "=" to your dynamic_method_name. In ruby, :post_id is the name of the getter, and :post_id= is the name of the setter. The .to_sym isn't strictly necessary so you can do this:

@model.send("#{dynamic_method_name}=", 123)
Andrew Vit
perfect. thanks!
bandhunt