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
Post a Comment