Hello all, I''m currently having an issue with some specs, specifically scope issues (probably more of a scope issue than a spec issue at this point)... Here''s the error: ActiveRecord::AssociationTypeMismatch in ''A new User should know what role it has'' Role expected, got NilClass D:/ruby/projects/restful_authentication_test/config/../app/models/role.rb:5:in `add'' ./spec/models/user_spec.rb:62: script/spec:4: Here''s the user.rb http://pastie.caboo.se/87029 role.rb http://pastie.caboo.se/87030 user_spec.rb http://pastie.caboo.se/87032 The code is based off of RESTful authentication + Ben Curtis'' user roles in Rails Application: http://www.bencurtis.com/archives/2007/06/user-roles-in-rails-applications/ Thanks for your help! -- dp -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070812/38430919/attachment.html
On 8/12/07, David Parker <nemo7467 at gmail.com> wrote:> Hello all, I''m currently having an issue with some specs, specifically scope > issues (probably more of a scope issue than a spec issue at this point)... > Here''s the error: > ActiveRecord::AssociationTypeMismatch in ''A new User should > know what role it has'' > Role expected, got NilClass > D:/ruby/projects/restful_authentication_test/config/../app/models/role.rb:5:in > `add'' > ./spec/models/user_spec.rb:62: > script/spec:4: > > Here''s the user.rb > http://pastie.caboo.se/87029 > > role.rb > http://pastie.caboo.se/87030 > > user_spec.rb > http://pastie.caboo.se/87032 > > The code is based off of RESTful authentication + > Ben Curtis'' user roles in Rails Application: > http://www.bencurtis.com/archives/2007/06/user-roles-in-rails-applications/users.roles is an AssociationProxy, not the Role class, where you have defined the method add(user, role). You should be able to eliminate that method in the Role class and just pass the role on line 62 of your spec: users.roles.add(role)> > Thanks for your help! > -- > dp > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 8/12/07, David Parker <nemo7467 at gmail.com> wrote:> Hello all, I''m currently having an issue with some specs, specificallyscope> issues (probably more of a scope issue than a spec issue at this point)... > Here''s the error: > ActiveRecord::AssociationTypeMismatch in ''A new User should > know what role it has'' > Role expected, got NilClass >D:/ruby/projects/restful_authentication_test/config/../app/models/role.rb:5:in> `add'' > ./spec/models/user_spec.rb:62: > script/spec:4: > > Here''s the user.rb > http://pastie.caboo.se/87029 > > role.rb > http://pastie.caboo.se/87030 > > user_spec.rb > http://pastie.caboo.se/87032 > > The code is based off of RESTful authentication + > Ben Curtis'' user roles in Rails Application: > http://www.bencurtis.com/archives/2007/06/user-roles-in-rails-applications/>users.roles is an AssociationProxy, not the Role class, where you have defined the method add(user, role). You should be able to eliminate that method in the Role class and just pass the role on line 62 of your spec: users.roles.add(role) ____________ I went ahead an eliminated the add method in the role class, and now, with the same spec as above these are the results I get, depending on what I have for line 62... Users.roles.add(role): NameError in ''A new User should know what role it has'' uninitialized constant Users User.roles.add(role): NoMethodError in ''A new User should know what role it has'' undefined method `roles'' for User:Class user.roles.add(role): NoMethodError in ''A new User should know what role it has'' undefined method `add'' for Role:Class users.roles.add(role): NameError in ''A new User should know what role it has'' undefined local variable or method `users'' If I go ahead and do something like: users = User.find(:all) before doing users.roles.add(role), then I get: NoMethodError in ''A new User should know what role it has'' undefined method `roles'' are my associations somehow not registering?> > Thanks for your help! > -- > dp > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- dp -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070813/954f6dcb/attachment.html
On 8/13/07, David Parker <nemo7467 at gmail.com> wrote:> On 8/12/07, David Parker <nemo7467 at gmail.com> wrote: > > Hello all, I''m currently having an issue with some specs, specifically > scope > > issues (probably more of a scope issue than a spec issue at this point)... > > Here''s the error: > > ActiveRecord::AssociationTypeM > ismatch in ''A new User should > > know what role it has'' > > Role expected, got NilClass > > > D:/ruby/projects/restful_authentication_test/config/../app/models/role.rb:5:in > > `add'' > > ./spec/models/user_spec.rb:62: > > script/spec:4: > > > > Here''s the user.rb > > http://pastie.caboo.se/87029 > > > > role.rb > > http://pastie.caboo.se/87030 > > > > user_spec.rb > > http://pastie.caboo.se/87032 > > > > The code is based off of RESTful authentication + > > Ben Curtis'' user roles in Rails Application: > > > http://www.bencurtis.com/archives/2007/06/user-roles-in-rails-applications/ > > > users.roles is an AssociationProxy, not the Role class, where you have > defined the method add(user, role). You should be able to eliminate > that method in the Role class and just pass the role on line 62 of > your spec: > > users.roles.add(role) > ____________ > I went ahead an eliminated the add method in the role class, and now, with > the same spec as above these are the results I get, depending on what I have > for line 62... >It looks like all of your questions are Ruby or Rails specific, not RSpec specific. There are other more suitable forums for that.> Users.roles.add(role): > NameError in ''A new User should know what role it has'' > uninitialized constant Users >This is Ruby telling you that there is no class named Users.> User.roles.add(role): > NoMethodError in ''A new User should know what role it has'' > undefined method `roles'' for User:Class >Only ActiveRecord instances (not classes) have associations. You can find out more by reading up on ActiveRecord associations.> user.roles.add(role): > NoMethodError in ''A new User should know what role it has'' > undefined method `add'' for Role:Class >I don''t think Rails has_many associations have an #add method. Use #<< or #build or #create http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000642> users.roles.add(role): > NameError in ''A new User should know what role it has'' > undefined local variable or method `users'' >This is Ruby telling you that you don''t have a users variable (or method) in the current scope.> If I go ahead and do something like: > users = User.find(:all) > before doing users.roles.add(role), then I get: > NoMethodError in ''A new User should know what role it has'' > undefined method `roles'' >I think you''re trying to add a role to *one* user, not all of them. A collection of User (which is what your users variable is) doesn''t have a roles method, but each of the User *instances* in that collection do. Try this: user = User.find(:first) user.should_not have_role(''master'') user.add_role(''master'') user.should have_role(''master'')> are my associations somehow not registering? >No, they''re registering fine, but I think you need to study Ruby and Rails/ActiveRecord more. You''re not using ActiveRecord properly. HTH, Aslak> > > > Thanks for your help! > > -- > > dp > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > -- > > dp > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >