The string parameters will be interpreted in the current context, so you're free to call self.name
there, or name
directly:
SomeClass.class_eval "has_many :#{name.tableize}, :through=>:join_models"
If instead of doing an eval on a string you were using a block, note that class_exec
is a variation on class_eval
that allows you to pass parameters naturally. So:
SomeClass.class_exec(name.tableize.to_sym) do |klass|
has_many klass, :through=>:join_models
end
It is new to 1.8.7, so you'll need to require 'backports/1.8.7'
if in 1.8.6.
Note: I'm assuming you need to eval at all, because in your particular example, you can call that method directly, no?
SomeClass.has_many name.tableize.to_sym, :through=>:join_models
Marc-André Lafortune
2010-04-09 16:00:58