I have a little gotcha (?) that confused me for a few moments. Let me explain: I have the following tables: members, roles, permissions. Now to do setup the relations, I have the following lookup tables: members_roles, permissions_roles (supposed to be alphabetical, correct?) In roles I have: has_and_belongs_to_many :permissions This looks for and finds the permissions_roles table. Here''s what got me...I need to create/destroy permissions_roles. I thought that would be easy by creating a permission_role model. However, this looks for a table named permission_roles (doesn''t pluralized the permission). I understand why this happens and now am curious as to the best practice. What is the rails way of managing the data in the permissions_roles table. Should I go through the role model? How would I delete a permissions_roles row by going through the role model? thanks, andy -- Andrew Stone _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Andrew Stone wrote:> I have a little gotcha (?) that confused me for a few moments. Let me > explain:<snip>> Here''s what got me...I need to create/destroy permissions_roles. I thought > that would be easy by creating a permission_role model. However, this looks > for a table named permission_roles (doesn''t pluralized the permission). I > understand why this happens and now am curious as to the best practice. What > is the rails way of managing the data in the permissions_roles table. Should > I go through the role model? How would I delete a permissions_roles row by > going through the role model?According to the api, something like @role.permissions<<@permission and @role.permissions.delete(@permission) should do it, no? Have I missed a subtlety? I don''t think you should need a separate model for the permission_role... -- Alex
Forgot to mention that I just used the set_table_name method in the PermissionRole model to point to "permissions_roles". This works...just wondering if it''s the correct way. thanks again, andy -- Andrew Stone _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> According to the api, something like > @role.permissions<<@permission and @role.permissions.delete(@permission) > should do it, no? Have I missed a subtlety? I don''t think you should > need a separate model for the permission_role...Alex, Not sure at this moment. I don''t want to delete the permission (would it do that?) I just want to delete the relation in the permissions_roles table. thanks for the quick reply, andy -- Andrew Stone _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Andrew Stone wrote:>>According to the api, something like >>@role.permissions<<@permission and @role.permissions.delete(@permission) >>should do it, no? Have I missed a subtlety? I don''t think you should >>need a separate model for the permission_role... > > Alex, > > Not sure at this moment. I don''t want to delete the permission (would it do > that?) I just want to delete the relation in the permissions_roles table.With a has_and_belong_to_many relationship, the docs (from http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000430) have this to say: collection.delete(object, …) - removes one or more objects from the collection by removing their associations from the join table. This does not destroy the objects. As far as I can see, it''s what you''re after. Don''t take my word for it, though - it should be fairly quick to test. -- Alex
Yeah, I ran a quick test and it works just as expected. Nice. Still have a lot of cobwebs from java/jsp in my brain I''m trying to clean up. -andy -- Andrew Stone _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I would just like to add that it made the code sooo much cleaner. So, note to self: when my code starts looking a little messy...I''m probably doing something wrong. :) Matt, Alex .. thanks again for your help, andy -- Andrew Stone _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Andrew Stone wrote:> I would just like to add that it made the code sooo much cleaner. So, note > to self: when my code starts looking a little messy...I''m probably doing > something wrong. :) >That seems to be a hallmark of ruby, and rails specifically - if it looks good, it probably is, and if it doesn''t, it probably isn''t...> Matt, Alex .. thanks again for your help,No worries :-) -- Alex