Polymorphic join validation error on create #10583

Closed
jwaldrip opened this Issue May 12, 2013 · 9 comments

5 participants

@jwaldrip

I can seem to get this one to work. I keep getting validation errors when trying to create objects via the association.

ERROR

[6] pry(main)> Account.first.users.create!
  Account Load (0.6ms)  SELECT "account".* FROM "account" ORDER BY "account"."id" ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Account can't be blank
from /Users/jwaldrip/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/validations.rb:57:in `save!'

CODE

class Account < ActiveRecord::Base

  has_many :permitted_permissions, class_name: 'Permission', as: :permissible, inverse_of: :permissible
  has_many :users, through: :permitted_permissions, source: :accessible, source_type: 'User', inverse_of: :account

end

class User < ActiveRecord::Base

  has_one :account_accessible_permission, class_name: 'Permission', as: :accessible, inverse_of: :accessible
  has_one :account, through: :account_accessible_permission, source: :permissible, source_type: 'Account', inverse_of: :users

  validates_presence_of :account

end

class Permission < ActiveRecord::Base

  validates_uniqueness_of :accessible_id, scope: [:accessible_type, :permissible_id, :permissible_type]

  belongs_to :permissible, polymorphic: true
  belongs_to :accessible, polymorphic: true

end
@jwaldrip

Any ideas on this one?

@senny
Ruby on Rails member

I'll take a look if you put your example into an executable test-case. You can use this file as a foundation: https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb

@neerajdotname
Ruby on Rails member

Here is a much simpler test with simple has_many :through case.

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments
end
class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments

  validates_presence_of :physicians
end

Now if you try

Physician.create.patients.create! 

then it will blow up because when the new patient record is being created then it does not have any physician.

@jwaldrip Does it summarize your test ? If yes then I'm not sure what can be done about it.

@jwaldrip

Yes, but if the physician is created then it should be passed as an attribute to patient upon create since its bring created via the association. This is something I thought :inverse_of would take care of.

@senny
Ruby on Rails member

There is a ticket with a similar description. As far as I know AR has_many .through associations do not support creation through the AssociationProxy, which makes this more a feature request than a bug report.

@neerajdotname correct me if I'm wrong 😄

@neerajdotname
Ruby on Rails member

@jwaldrip inverse_of does something different.

@physician = Physician.create!
@physician.patients.create!
@physician2 = @physician.patients.last

Without inverse_of @physician and @physician2 are two separate instances.

@seny for hm:t to work create must be invoked on collection_proxy and not on relation. For example

@physician.patients returns collection_proxy but @physician.patients.where(status: :active) would return relation and invoking create on that will not create association record. Not sure if this is what you were referring to.

@jwaldrip jwaldrip added the stale label Apr 23, 2014
@rafaelfranca
Ruby on Rails member

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this May 27, 2014
@rails-bot

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment