Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing the names of associated models

class Article < ActiveRecord::Base   has_many :comments   belongs_to :category end 

Is there a class method for Article with which I can retrieve a list of associations? I know by looking at the model's code that Article is associated to Comment and Category. But is there a method to get these associations programmatically?

like image 490
primary0 Avatar asked Apr 20 '11 15:04

primary0


People also ask

What are the top 10 modeling agencies in the world?

List of modeling agencies around the world in alphabetical order: Avenue 1 Co. Ltd. Cameron's Management Pty Ltd. Deco Models Inc. GO! Models M Inc. Nova Ltd. Prime Agency Co. Ltd. Select Model Management Ltd. Team Inc.

What are the model names used in the sample data?

The sample data provided with ArcFM Solution makes extensive use of model names. These model names are divided into two required domains: the Field Model Name Domain and the Object Class Model Name Domain.

What is a connectable model name?

This model name is also critical to tracing, Conduit Manager, and reporting. Some examples of connectable objects include Fiber, BacksidePort, FrontsidePort, DevicePort, SplitterInputPort, and SplitterOutputPort. Any object that should be connectable in the Connection Manager.

How do I assign a model name in ArcFM solution desktop?

In ArcFM Solution Desktop: This model name must be assigned to pipe feature classes that participate in cathodic protection systems. If this model name is assigned to ArcFM System Tables, all tabs in the Properties Manager will be displayed. By default only the Model Names and Field Model Names tabs are displayed for System Tables.


1 Answers

You want ActiveRecord::Reflection::ClassMethods#reflect_on_all_associations

So it would be:

 Article.reflect_on_all_associations 

And you can pass in an optional parameter to narrow the search down, so:

 Article.reflect_on_all_associations(:has_many)   Article.reflect_on_all_associations(:belongs_to) 

Keep in mind that if you want the list of all the names of the models you can do something like:

Article.reflect_on_all_associations(:belongs_to).map(&:name) 

This will return a list of all the model names that belong to Article.

like image 100
Mike Lewis Avatar answered Oct 16 '22 02:10

Mike Lewis