I am trying to do this, where associated_record_type is always of the name of a model, passed to the method:
record = eval(“#{associated_record_type}”).find_by_id(associated_record_id)
Is there a way to do this dynamically without using an eval?
I am trying to do this, where associated_record_type is always of the name of a model, passed to the method:
record = eval(“#{associated_record_type}”).find_by_id(associated_record_id)
Is there a way to do this dynamically without using an eval?
record = associated_record_type.classify.constantize.find_by_id(associated_record_id)
Actually, you might be better off with just .constantize - depends what precise value you have in "associated_record_type"
record = associated_record_type.constantize.find_by_id(associated_record_id)
http://api.rubyonrails.org/v2.3.8/classes/ActiveSupport/CoreExtensions/String/Inflections.html
record = eval(“#{associated_record_type}”).find_by_id(associated_record_id)
Is there a way to do this dynamically without using an eval?
record = associated_record_type.classify.constantize.find_by_id(associated_record_id)
Actually, you might be better off with just .constantize - depends
what precise value you have in “associated_record_type”
Michael, thats just what I was looking for. It is a polymorphic situation and the value in the field is a string, so works out perfect. Thanks!