Forum: Ruby on Rails Obtaining table information from "belongs_to"? (reflection?)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vic (Guest)
on 2007-03-19 16:43
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
Kevin (Guest)
on 2007-03-20 11:06
(Received via mailing list)
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::Reflec...

"""
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
This topic is locked and can not be replied to.