jeudi 29 octobre 2015

Get rails association from foreign key?

What I want: for a given foreign key name, and the model class, I can get the association (to know which model it can be resolved to).

For example:

# model: product.rb
class Product < ActiveRecord::Base
  belongs_to :category
end

# resolution:
association = Product.get_association('category_id')

So I need this get_association function.

What I know now:

  1. from Product.reflections I can get a list of reflections / associations
  2. for one reflection, I can get the foreign key
  3. I can build a map for this foreign key, to get the association

However, I want to ask if there is a simple method I can call directly?





Aucun commentaire:

Enregistrer un commentaire