17

I want to know field name corresponding to table caption for a given model in Rails.

I am displaying captions using a query model.

query.columns.map{|q| q.caption}
=> ["Tracker", "Status", "Priority", "Subject", "Assignee", "Target version", "Due date", "% Done"]

Column has names corresponding to captions

query.columns.map{|q| q.name}
=> [:tracker, :status, :priority, :subject, :assigned_to, :fixed_version, :due_date, :done_ratio]

My model looks like

Issue.columns.map{|q| q.name}
=> ["id", "tracker_id", "project_id", "subject", "description", "due_date", "category_id", "status_id", "assigned_to_id", "priority_id", "fixed_version_id", "author_id", "created_on", "updated_on", "start_date", "done_ratio", "estimated_hours", "parent_id"]

I want to get field name(the db field name) corresponding to a caption from the above information.

Sample association in the model

belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id'

So for above association i want to know the foreign key.

for assigned_to i want 'assigned_to_id'

Dipendra Singh
  • 542
  • 1
  • 6
  • 23

3 Answers3

27

The reflections hash holds this kind of information:

Issue.reflections['assigned_to'].foreign_key

You can also get other information, such as the class (.active_record) or the type of association (.macro). Prior to rails 4.2, the keys of this hash are symbols and not strings.

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • this worked for me...Issue.reflections[:assigned_to].options[:foreign_key].. thanks – Dipendra Singh Sep 07 '12 at 11:47
  • This would only work out if the relation is defined with the active record model and if there is any deviation with the Rails convention of naming the foreign key fields then that should also be specified in the active record model or else it just returns the field name by convention and not the actual field name. – Richa Sinha Feb 11 '20 at 05:16
1

The correct way for Rails 4.2 is:

Issue.reflections['assigned_to'].options[:foreign_key]

Note that "assigned_to" is a string according to API:

Returns a Hash of name of the reflection as the key and a AssociationReflection as the value.

http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflections

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
  • 1
    Note: This will return only the custom foreign keys attached to the options hash passed to the association helper. So it will not work in the standard cases where the foreign key is automatically set. BTW Frederick's Solution still works in Rails 4.2 – MhdSyrwan Jul 20 '15 at 08:33
  • 1
    Indeed! Thank you for noting! Though, to make Frederick's solution work in Rails 4.2, association name must be passed as a string instead of a symbol. – Artur INTECH Jul 21 '15 at 01:06
0

I'm posting this here in case this helps anyone else. I was trying to get the foreign key from proxy_association inside of an association extension.

You can get the reflection by simply using proxy_associtation.reflection.

Then, using @Frederick Cheung's method above, you can get the foreign_key like so:

reflection = proxy_association.reflection
reflection.foreign_key

You can also get the class_name in the same way:

reflection = proxy_association.reflection
reflection.class_name
daven97
  • 21
  • 1
  • 4