Error while using `find_or_create_by` on a `has_many` `through` association
Asked Answered
I

1

6

I am running in to a problem while using find_or_create_by on a has_many through association.

class Permission < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end

class Role < ActiveRecord::Base
  # DB columns: user_id, role_id

  has_many :permissions
  has_many :users, :through => :permissions
end

class User
  has_many :permissions
  has_many :roles, :through => :permissions
end

Rails throws an error when I invoke find_or_create_by on roles association of a User object.

u = User.first
u.roles.find_or_create_by_rolename("admin")

# Rails throws the following error
# NoMethodError: undefined method `user_id=' for #<Role id: nil, rolename: nil, 
#  created_at: nil, updated_at: nil>

I was able to work around the problem by changing my code as follows:

unless u.roles.exists?(:rolename => "admin")
  u.roles << Role.find_or_create_by_rolename("admin") 
end

I am curious to find if find_or_create_by works with has_many through associations.

Impatience answered 9/2, 2010 at 21:36 Comment(0)
G
1

It works, but not with :through.

Govern answered 9/2, 2010 at 21:50 Comment(5)
Yes, the problem is confined to :through. I will update the question to reflect that.Impatience
I don't think that you will get any more answers on this one. The find_or_... methods aren't supposed to work with the :through associations. The only way you could get it working would be by deleting the Permission model and use a has_and_belongs_to_many relationship with a simple mapping-table.Govern
Calls such as u.roles.find_by_rolename("admin") works with has_many :through. So I thought u.roles.find_or_create_by_rolename("admin") might work. Can you point me to the documentation where this caveat is specified?Impatience
I would be happy to prove this issue, but the find_by-methods aren't documented very well. However I think it's easy to understand why the "create" part does not work (while "find" does): The create would need to guess/generate a mapping "Permission". Rails can do this with habtm but not with complex types (what if you have validators in Permissions? You would be pretty screwed.)Govern
The create call like u.roles.create(:rolename => "admin") works. Rails creates the Permission map automatically. I expected find_or_create to work similarly(assuming that there is no validation errors). At this stage I am curious to know if it is a conscious omission OR a bug.Impatience

© 2022 - 2024 — McMap. All rights reserved.