hitesh israni
hitesh israni

Reputation: 1752

ActiveRecord belongs_to reverse mapping. Getting symbol associated with foreign key

I have a model say Issue(ActiveRecord) it has some fields viz. priority_id, status

:status, :class_name => 'IssueStatus', :foreign_key => 'status_id'

is there a way to know at run time- a field belongs to what model?

in above example, how to know the symbol associated with :foreign_key => 'status_id'

for instance, i want to get :status and i have 'status_id' at run time

comments please.

thanks

Upvotes: 0

Views: 225

Answers (1)

Khaja Minhajuddin
Khaja Minhajuddin

Reputation: 6741

You can always use issue.status.class to show the class of the status field (where issue is an object of the Issue model), Note that this is not specific to ActiveRecord, you can call the class method on any object to find it's class/type.

Upvotes: 3

Related Questions