Is there a way to instantiate a namespaced model object from its table name?
eg: Given table school_students
, model School::Student
, id 30
, I could do:
student = get_from_table_name_and_id("school_students", 38)
Is there a way to instantiate a namespaced model object from its table name?
eg: Given table school_students
, model School::Student
, id 30
, I could do:
student = get_from_table_name_and_id("school_students", 38)
This implementation should work
def get_from_table_name_and_id(klass, id)
klass.gsub('_','/').classify.constantize.find(id)
end
an output :
irb(main):004:0> "school_students".gsub('_','/').classify
=> "School::Student"
The naming convention on Rails is a namespace is represent by a /
in String