Instance Class o ActiveRecord Model using a String

class_name = "Post" my_class = Object.const_get(class_name) my_object = my_class.new # => "Post" my_object.class.name

1 Response

Its ok. another way is "Post".constantize.first

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.