activerecord - Rails: Belongs_to Polymorphic Association + conditions -
activerecord - Rails: Belongs_to Polymorphic Association + conditions -
so have model:
class model < activerecord::base attr_accessible :to_id, :to_type belongs_to :to, polymorphic: true end
i wondering if add together relationship when belongs_to on specific type:
class model < activerecord::base attr_accessible :to_id, :to_type belongs_to :to, polymorphic: true belongs_to :to_user, :foreign_key => :to_id, :conditions => ['to_type = ?', 'user'] # doesn't work # or maybe belongs_to :to_user, :foreign_key => :to_id, :foreign_class => 'user' # doesn't check on model's to_type... end
so my_model.to_user
homecoming user
if exists, , nil
if unset or of different class.
using rails 3.2
thanks!
you can utilize status within
belongs_to :to_user,-> {where(:to_type=> 'user')}, :foreign_key => :to_id
form more have on association callbacks
ruby-on-rails-3 activerecord relationships
Comments
Post a Comment