I have added roles and roles_users table and updated the model so that my users can have multiple roles. ("Admin" role does always have id = 1). I have added these methods to my application controller. <code> helper_method :is_admin? helper_method :is_user? def is_admin? if @session[''user''] @session[''user''].roles.find(1) else false end end def is_user? !@session[''user''].nil? end </code> The problem is that if a user does not have the role "admin" (id=1) the @session[''user''].roles.find(1) statment fails with error "unable to find role with id = 1". How can I do this another way..... I know it must be simple... but I''m really a Rails/Ruby newbie ;) Regards // JoNtE
On 10.3.2005, at 17:35, Jonas Montonen wrote:> > The problem is that if a user does not have the role "admin" (id=1) the > @session[''user''].roles.find(1) statment fails with error "unable to > find role with id = 1".This is because find assumes that the record with given id exists, i.e. that user knows there should be such row in the db. Therefore not finding that row is an exceptional state. find_all is a method that doesn''t assume any number of rows returned. However, in your case @session[''user''].roles is an array so you can do something like this: @session[''user''].roles.collect{ |role| role.id }.include?(1) //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Thursday 10 March 2005 10:35, Jonas Montonen wrote:> The problem is that if a user does not have the role "admin" (id=1) the > @session[''user''].roles.find(1) statment fails with error "unable to find > role with id = 1".You''re probably looking for find_first. -- Nicholas Seckar aka. Ulysses
Jonas Montonen wrote:> I have added roles and roles_users table and updated the model so that > my users can have multiple roles. > ("Admin" role does always have id = 1). > > I have added these methods to my application controller. > <code> > helper_method :is_admin? > helper_method :is_user? > > def is_admin? > if @session[''user''] > @session[''user''].roles.find(1) > else > false > end end > > def is_user? > !@session[''user''].nil? > end > </code> > > The problem is that if a user does not have the role "admin" (id=1) the > @session[''user''].roles.find(1) statment fails with error "unable to > find role with id = 1". > > How can I do this another way..... I know it must be simple... but I''m > really a Rails/Ruby newbie ;) > > Regards > // JoNtE > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >@session[''user''].roles.include?(1) Workd for me... I guess both solutions suggested would have worked as well. Thanx Jarkko and Nicholas for the quick answers // JoNtE
Jonas Montonen wrote:> Jonas Montonen wrote: > >> I have added roles and roles_users table and updated the model so >> that my users can have multiple roles. >> ("Admin" role does always have id = 1). >> >> I have added these methods to my application controller. >> <code> >> helper_method :is_admin? >> helper_method :is_user? >> >> def is_admin? >> if @session[''user''] >> @session[''user''].roles.find(1) >> else >> false >> end end >> >> def is_user? >> !@session[''user''].nil? >> end >> </code> >> >> The problem is that if a user does not have the role "admin" (id=1) the >> @session[''user''].roles.find(1) statment fails with error "unable to >> find role with id = 1". >> >> How can I do this another way..... I know it must be simple... but >> I''m really a Rails/Ruby newbie ;) >> >> Regards >> // JoNtE >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > @session[''user''].roles.include?(1) > Workd for me... I guess both solutions suggested would have worked as > well. > > Thanx Jarkko and Nicholas for the quick answers > > // JoNtE > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Well... after som testing my solution did not work after all... but Jarkko''s did. Great! @session[''user''].roles.collect{ |role| role.id }.include?(1) // JoNtE _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 10.3.2005, at 18:17, Jonas Montonen wrote:> > Well... after som testing my solution did not work after all... but > Jarkko''s did. > Great! > > @session[''user''].roles.collect{ |role| role.id }.include?(1)The reason is that the roles array consists of role objects, not id''s. It might still work if Role objects had a to_int method that would return their id, but I don''t know if that''s the case. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I actually use very similar code in snowdevil. But i wrapped it in a model method and access it as user.role?("admin") On Thu, 10 Mar 2005 18:25:11 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 10.3.2005, at 18:17, Jonas Montonen wrote: > > > > Well... after som testing my solution did not work after all... but > > Jarkko''s did. > > Great! > > > > @session[''user''].roles.collect{ |role| role.id }.include?(1) > > The reason is that the roles array consists of role objects, not id''s. > It might still work if Role objects had a to_int method that would > return their id, but I don''t know if that''s the case. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
On Thu, Mar 10, 2005 at 04:35:40PM +0100, Jonas Montonen wrote:> I have added roles and roles_users table and updated the model so that > my users can have multiple roles. > ("Admin" role does always have id = 1). > > I have added these methods to my application controller. > <code> > helper_method :is_admin? > helper_method :is_user? > > def is_admin? > if @session[''user''] > @session[''user''].roles.find(1) > else > false > end > end > > def is_user? > !@session[''user''].nil? > end > </code> > > The problem is that if a user does not have the role "admin" (id=1) the > @session[''user''].roles.find(1) statment fails with error "unable to find > role with id = 1". > > How can I do this another way..... I know it must be simple... but I''m > really a Rails/Ruby newbie ;)In addition to the recommendations that jarkko and seckar offered here are two other ways to get around find raising an exception. one way is to (ab)use the dynamic finders. def is_admin? @session[''user''] ? @session[''user''].roles.find_by_id(1) : false end Another way is to use rescue. def is_admin? @session[''user''].roles.find(1) rescue false end Though don''t use rescue cavalierly. You could cover up what might be an unrelated exception. marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
On Saturday 12 March 2005 11:18, Marcel Molina Jr. wrote:> Though don''t use rescue cavalierly. You could cover up what might be an > unrelated exception.If you''re both lazy and worried about unrestricted rescues, use supress: class Object def suppress(*exc_classes) begin return yield rescue Object => e raise unless exc_classes.any? {|cls| e.kind_of? cls} end end end suppress {raise ArgumentError} # raises argument error past suppress suppress(ArgumentError) {raise ArgumentError} # returns nil suppress(ArgumentError) { 2 } # returns 2 -- Nicholas Seckar aka. Ulysses