The documentation implies that inverse_of will work for everything except polymorphic associations. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
However it appears inverse_of still doesn't work for has_many :through
e.g. every combination I have tried for inverse_of on the follow example doesn't work
class Event < ActiveRecord::Base
has_many :attendances
has_many :users, through: :attendance
class User < ActiveRecord::Base
has_many :attendances
has_many :events, through: :attendances
class Attendance < ActiveRecord::Base
belongs_to :event
belongs_to :user
any ideas of this should work? and if so how I would set inverse_of in this example?
e.g. of what i tried ( also tried on has_many :through)
class Event < ActiveRecord::Base
has_many :attendances , :inverse_of => :event
has_many :users, through: :attendance
end
class User < ActiveRecord::Base
has_many :attendances , :inverse_of => :user
has_many :events, through: :attendances
end
class Attendance < ActiveRecord::Base
belongs_to :event, :inverse_of => :attendances
belongs_to :user, :inverse_of => :attendances
end
also tried
class Event < ActiveRecord::Base
has_many :attendances
has_many :users, through: :attendance , :inverse_of => :events
end
class User < ActiveRecord::Base
has_many :attendances
has_many :events, through: :attendances ,:inverse_of => :users
end
class Attendance < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
I had the same question in rails 4.1.6 (that's how I stumbled upon this question).
It took a bit of trial and error (and server restarts), but the following works for me:
class User < ActiveRecord::Base
has_many :company_users
has_many :companies, through: :company_users
end
class Company < ActiveRecord::Base
has_many :company_users
has_many :users, through: :company_users
end
class CompanyUser < ActiveRecord::Base
belongs_to :company, inverse_of: :company_users
belongs_to :user, inverse_of: :company_users
end
For clarity:
Example:
user = User.last
company = user.companies.build( ... )
company.save
# ...
# SQL (0.9ms) INSERT INTO "public"."company_users" ("company_id", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["company_id", 4], ["created_at", "2014-10-03 03:40:58.836975"], ["updated_at", "2014-10-03 03:40:58.836975"], ["user_id", 1]]
(1.6ms) COMMIT
CompanyUser.last
<CompanyUser:0x00000020339710> {
:id => 4,
:company_id => 4,
:user_id => 1,
:created_at => Thu, 02 Oct 2014 20:40:58 PDT -07:00,
:updated_at => Thu, 02 Oct 2014 20:40:58 PDT -07:00
}
So basically I only set the inverse_of in the join model
The guides say inverse_of is not supported when using :through. Specifically:
There are a few limitations to inverse_of support:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With