The error is because there is no resource associated at that point. It's a perfectly valid approach, though there are lots of potential pitfalls. Either you need to test for existence first, catch the exception or otherwise ensure there is always a valid association.
Simplest fix is probably to use resource.try.building.try.name where .try is a hobo object extension method that returns nil if the next method call doesn't exist on the object. This would ensure that the association exists as well since a nil return would count as false. On Jun 20, 10:44 am, Betelgeuse <[email protected]> wrote: > I have been testing if it's possible to use attributes of associated > objects in create_permitted? like this (just an imaginary example): > > belongs_to :resource > > def create_permitted? > resource.building.name == 'Foo' > end > > This gives nil when trying to open up a form for creating new entries: > > You have a nil object when you didn't expect it! > The error occurred while evaluating nil.building > > RAILS_ROOT: /home/betelgeuse/hobotest > Application Trace | Framework Trace | Full Trace > > /home/betelgeuse/hobotest/app/models/users_resource.rb:16:in > `create_permitted?' > app/views/taglibs/auto/rapid/pages.dryml:358:in > `index_page__for_users_resource' > app/views/taglibs/auto/rapid/pages.dryml:356:in > `index_page__for_users_resource' > app/views/taglibs/auto/rapid/pages.dryml:356:in > `index_page__for_users_resource' > app/views/taglibs/auto/rapid/pages.dryml:349:in > `index_page__for_users_resource' > app/views/taglibs/themes/clean/clean.dryml:2:in `page' > app/views/taglibs/themes/clean/clean.dryml:1:in `page' > app/views/taglibs/auto/rapid/pages.dryml:346:in > `index_page__for_users_resource' > app/views/taglibs/auto/rapid/pages.dryml:345:in > `index_page__for_users_resource' > > Should I be able to use associations like this and I have stumbled on > a bug or is what I am trying to do invalid? This is with hobo-0.8.7. I > can write something to the manual if the associations are expected to > be nil in create_permitted?. When thinking about it for the framework > to be able to do New links properly etc it would make sense that > checking like this is not possible. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Hobo Users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/hobousers?hl=en -~----------~----~----~----~------~----~------~--~---
