sql - ActiveRecord polymorphic has_many with ActiveSupport::Concern -


i have following concern:

module eventable   extend activesupport::concern   # ...   included     has_many :subscriptions, as: :entity, dependent: :destroy   end end 

my models are:

class experiment < activerecord::base   include eventable end  class subscription < activerecord::base   belongs_to :entity, polymorphic: true end 

in controller try create subscription experiment, following:

class subscriptionscontroller < applicationcontroller   before_filter :find_entity    def create     subscription = subscriptions.new(params[:subscription])     @entity.subscriptions << subscription # why false?     # ...   end  end 

but doesn't work.

while debugging, noticed @entity.subscriptions.count create incorrect sql query:

select count(*) [subscriptions] [subscriptions].[experiment_id] = 123 

while expect:

select count(*) [subscriptions] [subscriptions].[entity_id] = 123 , [subscriptions].[entity_type] = 'experiment' 

note: if following, works correctly:

subscription.entity = @entity subscription.save 

thanks help!

the reason error: class experiment (not class) had has_many :subscriptions

advise: if have strange behavior, , use others people code, stop , review code!


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -