Hi. I am frustrated here. I cannot access the attributes of the relational model. This should be straight forward and simple, I guess that''s why it''s frustrating. Let''s start with the models: ##### Models ##################### class Ad < ActiveRecord:Base belongs_to :book class Book < Inventory has_one :ad (i''ve also played with has_many, but same scenario) ##### Ads Controller ############################################### @ads = Ad.find_all_by_user_id session[:user] #grab all ads from current user ##### View ######################################################### <% @ads.each do |ad| %> <%= ad.book.nil? %> (will say it''s false, but yet...) <%= ad.book.attributes %> (will give the following error: <% end %> "You have a nil object when you didn''t expect it! You might have expected an instance of ActiveRecord::Base. The error occured while evaluating nil.attributes" Can someone help? And happy New Years Eve Everyone! D -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dominic Son wrote:> <% @ads.each do |ad| %> > <%= ad.book.nil? %> (will say it''s false, but yet...) > <%= ad.book.attributes %> (will give the following error: > <% end %> > > "You have a nil object when you didn''t expect it! > You might have expected an instance of ActiveRecord::Base. > The error occured while evaluating nil.attributes"I would try looking at the objects with debug: <%= debug ad %> <%= debug ad.book %> Dan Manges --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yes, did that and all the objects are in fact in the @ads variable, yet I cannot access them.. :( Dan Manges wrote:> Dominic Son wrote: >> <% @ads.each do |ad| %> >> <%= ad.book.nil? %> (will say it''s false, but yet...) >> <%= ad.book.attributes %> (will give the following error: >> <% end %> >> >> "You have a nil object when you didn''t expect it! >> You might have expected an instance of ActiveRecord::Base. >> The error occured while evaluating nil.attributes" > > I would try looking at the objects with debug: > <%= debug ad %> > <%= debug ad.book %> > > Dan Manges-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> class Book < Inventory > has_one :adDominic Looks like you''re using STI? I''ve never done this with STI. No reason to think it wouldn''t work though. You might start with a simpler example not using STI - as in the Agile Web Dev book p. 231. Also, try using script/console to test things. Very handy tool for sussing this stuff out. The table structure, model, and code all need to be in sync. Good luck Joe -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Man, if this is the case, STI needs to be a lot more flexible. I couldnt use STI with an acts_as_tree also : ( (why would I do that? for a more human readable code to access the different node levels...) i do have a hint that this is the case, and i will try taking out the STI... Thanks Joe and Dan Joe Hartman wrote:>> class Book < Inventory >> has_one :ad > > Dominic > > Looks like you''re using STI? I''ve never done this with STI. No reason to > think it wouldn''t work though. You might start with a simpler example > not using STI - as in the Agile Web Dev book p. 231. > > Also, try using script/console to test things. Very handy tool for > sussing this stuff out. > > The table structure, model, and code all need to be in sync. > > Good luck > > Joe-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
My bad, STI does work. I had deleted inventory that @ads was trying to call, that''s why it returned nil. It''s not nil on all returned entries, just a few... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---