Rails: Many to many polymorphic relationships

You can combine polymorphism and has_many :through to get a flexible mapping: class Assignment < ActiveRecord::Base belongs_to :task belongs_to :target, :polymorphic => true end class Task < ActiveRecord::Base has_many :targets, :through => :assignment end class Store < ActiveRecord::Base has_many :tasks, :through => :assignment, :as => :target end class Vehicle < ActiveRecord::Base has_many :tasks, :through => … Read more

Laravel – Eager Loading Polymorphic Relation’s Related Models

Solution: It is possible, if you add: protected $with = [‘company’]; to both the Service and Product models. That way, the company relation is eager-loaded every time a Service or a Product is loaded, including when loaded via the polymorphic relation with History. Explanation: This will result in an additional 2 queries, one for Service … Read more

Rails: includes with polymorphic association

Edit 2: I’m now using rails 4.2 and eager loading polymorphism is now a feature 🙂 Edit: This seemed to work in the console, but for some reason, my suggestion of use with the partials below still generates N+1 Query Stack warnings with the bullet gem. I need to investigate… Ok, I found the solution … Read more

Why polymorphic association doesn’t work for STI if type column of the polymorphic association doesn’t point to the base model of STI?

Good question. I had exactly the same problem using Rails 3.1. Looks like you can not do this, because it does not work. Probably it is an intended behavior. Apparently, using polymorphic associations in combination with Single Table Inheritance (STI) in Rails is a bit complicated. The current Rails documentation for Rails 3.2 gives this … Read more

ActiveRecord – querying polymorphic associations

Argh! I think I found the problem. When joining via: @comments = Comment.find(:all, :joins => “forum_topics”, :conditions => [“forum_topics.featured = ? “, true] ) You need the whole join! :joins => “INNER JOIN forum_topics ON forum_topics.id = comments.commentable_id”, See the ever-awesome: http://guides.rubyonrails.org/active_record_querying.html#joining-tables

Rails: has_many through with polymorphic association – will this work?

You have to do: class Person < ActiveRecord::Base has_many :events has_many :meals, :through => :events, :source => :eventable, :source_type => “Meal” has_many :workouts, :through => :events, :source => :eventable, :source_type => “Workout” end This will enable you to do this: p = Person.find(1) # get a person’s meals p.meals.each do |m| puts m end # … Read more

accepts_nested_attributes_for with belongs_to polymorphic

I’ve also had a problem with the “ArgumentError: Cannot build association model_name. Are you trying to build a polymorphic one-to-one association?” And I found a better solution for this kind of problem. You can use native method. Lets look to the nested_attributes implementation, inside Rails3: elsif !reject_new_record?(association_name, attributes) method = “build_#{association_name}” if respond_to?(method) send(method, attributes.except(*UNASSIGNABLE_KEYS)) … Read more

Rails Polymorphic Association with multiple associations on the same model

I have done that in my project. The trick is that photos need a column that will be used in has_one condition to distinguish between primary and secondary photos. Pay attention to what happens in :conditions here. has_one :photo, :as => ‘attachable’, :conditions => {:photo_type => ‘primary_photo’}, :dependent => :destroy has_one :secondary_photo, :class_name => ‘Photo’, … Read more

MySQL – Conditional Foreign Key Constraints

You’re attempting to do a design that is called Polymorphic Associations. That is, the foreign key may reference rows in any of several related tables. But a foreign key constraint must reference exactly one table. You can’t declare a foreign key that references different tables depending on the value in another column of your Comments … Read more