I have a has_many_through association where Users have many Projects through ProjectUsers. Rails has some magic that allows updating this relationship with:
u = User.first
u.update(project_ids: [...])
Is there a clean way to do the same thing with create?
Running User.create(name: ..., project_ids: [...]) fails with Validation failed: Project users is invalid.
I suspect this is because Rails tries to create the ProjectUser record before creating the User record and has some built-in validation on join tables to validate that both sides of the join already exist. The ProjectUser model has no custom validation.
class ProjectUser < ApplicationRecord
belongs_to :project
belongs_to :user
end
Is there a simple way to get around this?
Active Record supports automatic identification for most associations with standard names. However, Active Record will not automatically identify bi-directional associations that contain the :through or :foreign_key options. (You can check here)
So you have to define
inverse_ofexplicitly.