Obtaining table information from "belongs_to"? (reflection?)


#1

Hi all,

How would one obtain the database table name if given a column name?
Does ActiveRecord know how to do that?

For example if there is a model that “belongs_to :myobject_id”, is there
a way to get the table name from “myobject_id” ?

Thank you,
Vic


#2

On Mar 19, 7:43 am, Vic removed_email_address@domain.invalid wrote:

How would one obtain the database table name if given a column name?
Does ActiveRecord know how to do that?

From http://railsmanual.org/module/ActiveRecord::Reflection::ClassMethods/reflect_on_association/1.1.2

“”"
Returns the AssociationReflection object for the named aggregation
(use the symbol). Example:

Account.reflect_on_association(:owner) # returns the owner
AssociationReflection
Invoice.reflect_on_association(:line_items).macro #
returns :has_many
“”"

Then you should be able to call table_name() on the
AssociationReflection:
Account.reflect_on_association(:owner).table_name # returns ‘account’

Kevin