Using ActiveScaffold with the rails-authorization-plugin

3 views
Skip to first unread message

Dovadi

unread,
Mar 11, 2009, 6:19:38 AM3/11/09
to ActiveScaffold : Ruby on Rails plugin
I'm struggling to get ActiveScaffold working with users, roles and
has_many through association between them (I'm using the rails-
authorization-plugin).

class Role < ActiveRecord::Base
has_many :roles_users, :dependent => :delete_all
has_many :users, :through => :roles_users
belongs_to :authorizable, :polymorphic => true
end

class User < ActiveRecors::Base
has_many :roles_users, :dependent => :delete_all)
has_many :roles, :through => :roles_users
end

class UsersController < ApplicationController
active_scaffold do |config|
config.columns = [:email, :active, :roles]
end
end

I see the roles of the user as a link (http://local.host:3000/admin/2/
nested?_method=get&associations=roles) in my view, but when I follow
this link I get an error:

ActiveRecord::StatementInvalid (Mysql::Error: Unknown column
'roles_users_roles.id' in 'field list': SELECT `roles`.`id` AS t0_r0,
`roles`.`name` AS t0_r1, `roles`.`authorizable_type` AS t0_r2,
`roles`.`authorizable_id` AS t0_r3, `roles`.`created_at` AS t0_r4,
`roles`.`updated_at` AS t0_r5, `users`.`id` AS t1_r0, `users`.`email`
AS t1_r1, `users`.`encrypted_password` AS t1_r2, `users`.`salt` AS
t1_r3, `users`.`token` AS t1_r4, `users`.`token_expires_at` AS t1_r5,
`users`.`email_confirmed` AS t1_r6, `users`.`active` AS t1_r7,
`roles_users_roles`.`id` AS t2_r0, `roles_users_roles`.`user_id` AS
t2_r1, `roles_users_roles`.`role_id` AS t2_r2,
`roles_users_roles`.`created_at` AS t2_r3,
`roles_users_roles`.`updated_at` AS t2_r4, `users_roles_users`.`id` AS
t3_r0, `users_roles_users`.`email` AS t3_r1,
`users_roles_users`.`encrypted_password` AS t3_r2,
`users_roles_users`.`salt` AS t3_r3, `users_roles_users`.`token` AS
t3_r4, `users_roles_users`.`token_expires_at` AS t3_r5,
`users_roles_users`.`email_confirmed` AS t3_r6,
`users_roles_users`.`active` AS t3_r7 FROM `roles` LEFT OUTER JOIN
`roles_users` ON (`roles`.`id` = `roles_users`.`role_id`) LEFT OUTER
JOIN `users` ON (`users`.`id` = `roles_users`.`user_id`) LEFT OUTER
JOIN `roles_users` roles_users_roles ON roles_users_roles.role_id =
roles.id LEFT OUTER JOIN `users` users_roles_users ON
`users_roles_users`.id = `roles_users_roles`.user_id WHERE (users.id
= '2') AND `roles`.id IN (2, 4) ORDER BY roles.`id` ASC):

The SQL statement is wrong, there is no roles_users_roles.id, but I
think I'm doing something wrong here and using the wrong approach.

In the end I want something with config.columns[:roles].form_ui
= :select in the users controller, but this fails to render (also a
sql error Unknown column 'users_users.user_id' in 'on clause':
SELECT ......)

Can someone help me out?

Thanks, Frank

Sergio Cambra .:: entreCables - Symbol Servicios Informáticos S.L. ::.

unread,
Mar 11, 2009, 7:39:15 AM3/11/09
to actives...@googlegroups.com
El Wednesday 11 March 2009 11:19:38 Dovadi escribió:
> I'm struggling to get ActiveScaffold working with users, roles and
> has_many through association between them (I'm using the rails-
> authorization-plugin).
>
> class Role < ActiveRecord::Base
> has_many :roles_users, :dependent => :delete_all
> has_many :users, :through => :roles_users
> belongs_to :authorizable, :polymorphic => true
> end
>
> class User < ActiveRecors::Base
> has_many :roles_users, :dependent => :delete_all)
> has_many :roles, :through => :roles_users
> end
>
> class UsersController < ApplicationController
> active_scaffold do |config|
> config.columns = [:email, :active, :roles]
> end
> end

Why don't you use habtm?
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :dependent => :delete_all
belongs_to :authorizable, :polymorphic => true
end

class User < ActiveRecors::Base
has_and_belongs_to_many :roles, :dependent => :delete_all
end

I think when you use has_many and has_many :through, intermediate table must
have an id, habtm tables doesn't have an id.
--
Sergio Cambra .:: entreCables S.L. ::.
Nicolás Guillén 6, locales 2 y 3. 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) ser...@entrecables.com

Dovadi

unread,
Mar 11, 2009, 9:06:23 AM3/11/09
to ActiveScaffold : Ruby on Rails plugin
Hi Sergio,

It does work when I change the relationship into habtm. I have only
one issue with the 'Add Existing' link:

ActionView::MissingTemplate (Missing template admin/roles/
_add_existing_form.erb in view path app/views) (I use activescaffold
for admin purposes)

But the disadvantage is that I have to patch an existing and much used
plugin authorization plugin (see http://github.com/DocSavage/rails-authorization-plugin/tree/master)
and that is something I want to avoid.

And of course it still leaves the question: 'does ActiveScaffold work
with has_many through relationships?' If so, what is the right way to
do it?

Thanks, Frank

On 11 mrt, 12:39, "Sergio Cambra .:: entreCables - Symbol Servicios

Sergio Cambra .:: entreCables - Symbol Servicios Informáticos S.L. ::.

unread,
Mar 11, 2009, 12:43:53 PM3/11/09
to actives...@googlegroups.com
El Wednesday 11 March 2009 14:06:23 Dovadi escribió:
> Hi Sergio,
>
> It does work when I change the relationship into habtm. I have only
> one issue with the 'Add Existing' link:
>
> ActionView::MissingTemplate (Missing template admin/roles/
> _add_existing_form.erb in view path app/views) (I use activescaffold
> for admin purposes)
>
> But the disadvantage is that I have to patch an existing and much used
> plugin authorization plugin (see
> http://github.com/DocSavage/rails-authorization-plugin/tree/master) and
> that is something I want to avoid.
>
> And of course it still leaves the question: 'does ActiveScaffold work
> with has_many through relationships?' If so, what is the right way to
> do it?
>
> Thanks, Frank

I haven't used has_many through, so I'm not sure about ActiveScaffold features
for has_many through. Anyway, I think it's a rails issue, not an
ActiveScaffold one, mostly queries used in ActiveScaffold are made by rails
using find with :includes, :joins and :conditions. I think intermediate table
in has_many :through must have an id, so roles_users table should have an id.

Dovadi

unread,
Mar 12, 2009, 7:42:45 AM3/12/09
to ActiveScaffold : Ruby on Rails plugin
Hi Sergio,

You're right the roles_users table need to have an id (and it didn't
in the rails-authentication-plugin see
http://github.com/DocSavage/rails-authorization-plugin/blob/6a71f147658b12883e18736c744b70db82c482d2/generators/role_model/templates/migration.rb
- :id=>false). With an id in the roles_users table it works better,
but there a few problems, but easily fixed with workarounds.

Thanks, Frank

On 11 mrt, 17:43, "Sergio Cambra .:: entreCables - Symbol Servicios
Reply all
Reply to author
Forward
0 new messages