I can seem to get this one to work. I keep getting validation errors when trying to create objects via the association.
 pry(main)> Account.first.users.create!
Account Load (0.6ms) SELECT "account".* FROM "account" ORDER BY "account"."id" ASC LIMIT 1
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!'
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
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
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
Any ideas on this one?
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
@senny here is the failing test => https://github.com/jwaldrip/ar_double_poly
Here is a much simpler test with simple has_many :through case.
class Physician < ActiveRecord::Base
has_many :patients, through: :appointments
class Patient < ActiveRecord::Base
has_many :physicians, through: :appointments
Now if you try
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.
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.
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 😄
@jwaldrip inverse_of does something different.
@physician = Physician.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.
This issue has been automatically marked as stale because it has not been commented on for at least
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.
This issue has been automatically closed because of inactivity.