-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change role join table association (HABTM) to a has_many through association #318
Comments
I believe this was a requested feature that the original creator was working on but never completed. It definitely would be an interesting addition. |
This code works. The only thing is, how to add configuration data to a new user record, for example in a view with nested attributes. First I need to add the role and then I can create nested attributes for the configuration. How do you add roles from a user view? |
👍 There is a pull request for has many through option: |
I found a workaround for using First you of course want to alter the migration: class RolifyCreateRoles < ActiveRecord::Migration
def change
create_table(:roles) do |t|
t.string :name
t.references :resource, :polymorphic => true
t.timestamps
end
create_table(:user_roles) do |t|
t.references :user
t.references :role
t.timestamps
end
add_index(:roles, :name)
add_index(:roles, [ :name, :resource_type, :resource_id ])
add_index(:user_roles, [ :user_id, :role_id ])
end
end Then setup the models: class Role < ApplicationRecord
has_many :user_roles
has_many :users, through: :user_roles
belongs_to :resource,
polymorphic: true,
optional: true
validates :resource_type,
inclusion: { in: Rolify.resource_types },
allow_nil: true
scopify
end
class UserRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
class User < ApplicationRecord
rolify
has_many :user_roles
has_many :roles, through: :user_roles
end The key is putting |
Thanks @maxcal. Note: Without altering any migration referencing UsersRole instead of UserRole should work as intended. class Role < ApplicationRecord
has_many :users_roles
has_many :users, through: :users_roles
belongs_to :resource,
polymorphic: true,
optional: true
validates :resource_type,
inclusion: { in: Rolify.resource_types },
allow_nil: true
scopify
end
class UsersRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
class User < ApplicationRecord
rolify
has_many :users_roles
has_many :roles, through: :users_roles
end |
Breaking the pluralization conventions feels hacky. Better to call the class UserRole and set 'table_name = "users_roles"' |
Agree, that would be nice to keep conventions. |
Hey mates, I tried this setup since I need to add additional columns in But, unfortunately it does not working using Rails 5.2.1 and Ruby 2.5.1
|
You have to declare the association you are going through first:
Not:
|
I have the same issue as @scverano on Rails 5.2.0 and my association declarations are definitely in the right order. I suspect this Rails issue which was fixed in rails/rails@652258e and released in 5.2.1. Doesn't seem to help though. Update: Seems to work with @ashiksp's solution so long as you do NOT set the table name with |
I also encountered the |
Nbarthel have you actually checked the association reflection? There is a very good chance that you are just overwriting your assocation with Rolifys out of box habtm assocation. |
Hi everybody! I just migrated from rails 4.2.1 to rails 6.0.3.1 and I'm having the same HasManyThroughOrderError issue as @scverano and I already tried all of the solutions mentioned in this post. My association declarations are in the right order, here is how my models look like:
I'm using: Does anyone know what else is happening? I already looked at several post on StackOverflow and all the solitions suggest to just declare the association you are going through first, but that didn't work for me. |
Hi everyone, It turns out that there is an existing bug in Rails, which will raise an Now, the has_and_belongs_to_many :roles So whatever is the order you use in the User model, adding a has_many :roles manually will be considered a duplicate and raise an So for now, overriding the default configuration (has_and_belongs_to_many) does not seem possible, not until Rolify really supports the has_many through option. |
Hi!
I want to add more data to the role join table. Actually rolify uses a HABTM table. Why don't change rolify to use a has_many through association? So, everybody can expand the role join table with custom data.
Here an example using a hstore (Postgresql) field:
Here the model:
The text was updated successfully, but these errors were encountered: