Hi,
I am trying to retrieve the class name of an object. When I tried using the const_get, i am getting the entire model's table structure. So I used the following code.
Code
def classname(object)
return object.class.to_s.split("(")[0]
end
def classrating(object_id)
classtype = classname(object_id)
return classtype
end
Script/Console
>> q = Question.new
=> #<Question id: nil, question_info: nil, profile_id: nil, rating: nil, created_at: nil, updated_at: nil>
>> Question.classname(q)
=> "Question"
>> Question.classrating(Question.classname(q))
=> "String"
>> q.class
=> Question(id: integer, question_info: string, profile_id: integer, rating: integer, created_at: datetime, updated_at: datetime)
As you can see, when Question.classname is called, it returns Question and when the same input i called from Question.classrating, it returns String. I am just returning the same output from the Question.classname.
Can you please tell me whether what am I doing wrong, that the value gets changed.
Thanks.