ruby on rails - Polymorphic and non-polymorphic associations on the same model -


i have log model polymorphic association , association user model:

class log < activerecord::base   belongs_to :loggable, polymorphic: true   belongs_to :user end 

in user model, have has_many association log model:

class user < activerecord::base   has_many :logs end 

i want have association in user model follows:

class user < activerecord::base   has_many :logs   has_many :logs, as: :loggable end 

but imagine 2 associations conflict each other (i don't know, didn't try)...

so right approach problem? or there better way of doing it?

how renaming 1 of associations this:

class user < activerecord::base   has_many :user_logs, class_name: 'log'   has_many :logs, as: :loggable end 

to fetch logs associated given user, set scope on log model this:

class log < activerecord::base   belongs_to :loggable, polymorphic: true   belongs_to :user    scope :for_user, lambda { |user|     where('user_id = :user_id or (loggable_type = "user" , loggable_id = :user_id)', user_id: user.id)   } end 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -