Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the class of an association based on the foreign key attribute only?

Short: I have an foreign key attribute and want to know what is the class (or reference table) of that foreign key field.

Context:

given 2 tables: users(id, [other fields]) and issues(id, user_id, assigned_to, [other fields])

Here is my active record of Issue (irrelevant parts are extracted)

class User < ActiveRecord::Base
  ...
end
class Issue < ActiveRecord::Base
  belongs_to :user
  belongs_to :assigned_user, :foreign_key => 'assigned_to', :class_name => 'User'
  ...
end

I want to make a user readable change logging. e.g. when changing assigned user I want to get a message like this: Assigned to is changed from Otto to Zoltan. ActiveRecord has the function changes which is a good starting point but it give me only reference ID-s. To translate into names I need to read user by id.

For association :user it is quite easy because I have to follow conventions only. But how to fetch the same info for assigned_to attribute (I want to make a general solution)? Is it possible to figure out whether we have association for the given attribute? Can we extract the class of that association?

like image 898
takacsot Avatar asked Jul 13 '10 07:07

takacsot


People also ask

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

What is a foreign key in Active Record?

Foreign keys ensure consistency between related database tables. The current database review process always encourages you to add foreign keys when creating tables that reference records from other tables. Starting with Rails version 4, Rails includes migration helpers to add foreign key constraints to database tables.

What is foreign key in Ruby on Rails?

A foreign key is a field in one database that uniquely identifies a row in another table. For this example, our foreign key in the expenses table will identify which category each expense should fall under.

What is a polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.


2 Answers

First, you can use reflect_on_association to get the metadata for the association you want. Then, from its result (which is a MacroReflection descendant) you can find out the class:

reflection = Issue.reflect_on_association(:assigned_user)
reflection.class # => ActiveRecord::Reflection::AssociationReflection
reflection.class_name # => 'User'

Refer to docs here and there.

like image 179
alex.zherdev Avatar answered Oct 03 '22 23:10

alex.zherdev


Thank you. Here is the final solution for the specific problem (for learning):

  def textalize_changes
    if changed?
      r = ""
      changes.keys.each do |k|
        r << "#{k.humanize} changed "
        r << "from `#{translate(k,changes[k][0])}` " 
        r << "to `#{translate(k,changes[k][1])}`"
        r << "<br/>"
      end
      r
    end
  end

  def translate(attr_name, attr_value)
    ass = self.class.reflect_on_all_associations(:belongs_to).reject{|a| attr_name != a.primary_key_name}
    if 1 == ass.length and !attr_value.blank?
      obj = ass[0].klass.find(attr_value)
      obj.send(:name)
    else
      attr_value
    end
  end
like image 21
takacsot Avatar answered Oct 03 '22 22:10

takacsot