Paul Jonathan Thompson
2008-Jul-07 20:48 UTC
Accessing a model from controller not working.
Hi could someone please put me right. I am not having any success at accessing my model from a controller.Please look at the code: Model: class Region < ActiveRecord::Base def self.get_region_code(region) find_by_region("#{region}") end end Controller code: @region = Region.get_region_code(@region_name) When I try to access the contents of @region puts @region.region It blows up with: NoMethodError in Admin supplierController#update You have a nil object when you didn''t expect it! The error occurred while evaluating nil.region Thankyou very much. Paul Thompson. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Tue, 8 Jul 2008, Paul Jonathan Thompson wrote:> > Hi could someone please put me right. I am not having any success at > accessing my model from a controller.Please look at the code: > > Model: > > class Region < ActiveRecord::Base > > def self.get_region_code(region) > find_by_region("#{region}") > endIf region is a string, then you don''t need to quote and interpolate it. That will just produce the same string again. I''m not sure why you need to wrap find_by_region. Can''t you just use that in the controller?> end > > Controller code: > > @region = Region.get_region_code(@region_name) > > When I try to access the contents of @region > > puts @region.region > > It blows up with: > > NoMethodError in Admin supplierController#update > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.regionThat means that your call to Region.get_region_code returned nil. You''d need to do some inspecting to see what''s in @region_name at the time of the call. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Paul Jonathan Thompson
2008-Jul-07 21:28 UTC
Re: Accessing a model from controller not working.
Hi David, Thank you for your advice. It worked when I changed the code in the controller to: @region = Region.find_by_region(@region_name) rather than the previous code. But now I am confused, because I would have said that I am doing the identical thing. Where is my thinking wrong? As I have used the previous method before (with a different model and controller) and it worked just fine. The reason that I used the previous method was that I thought it was the correct way to do it. Regards, Paul 2008/7/8 David A. Black <dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org>:> > Hi -- > > On Tue, 8 Jul 2008, Paul Jonathan Thompson wrote: > >> >> Hi could someone please put me right. I am not having any success at >> accessing my model from a controller.Please look at the code: >> >> Model: >> >> class Region < ActiveRecord::Base >> >> def self.get_region_code(region) >> find_by_region("#{region}") >> end > > If region is a string, then you don''t need to quote and interpolate > it. That will just produce the same string again. > > I''m not sure why you need to wrap find_by_region. Can''t you just use > that in the controller? > >> end >> >> Controller code: >> >> @region = Region.get_region_code(@region_name) >> >> When I try to access the contents of @region >> >> puts @region.region >> >> It blows up with: >> >> NoMethodError in Admin supplierController#update >> >> You have a nil object when you didn''t expect it! >> The error occurred while evaluating nil.region > > That means that your call to Region.get_region_code returned nil. > You''d need to do some inspecting to see what''s in @region_name at the > time of the call. > > > David > > -- > Rails training from David A. Black and Ruby Power and Light: > Intro to Ruby on Rails July 21-24 Edison, NJ > Advancing With Rails August 18-21 Edison, NJ > See http://www.rubypal.com for details and updates! > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-Jul-07 22:12 UTC
Re: Accessing a model from controller not working.
Paul Jonathan Thompson wrote:> Hi David, > > Thank you for your advice. It worked when I changed the code in the > controller to: > > @region = Region.find_by_region(@region_name) > > rather than the previous code. But now I am confused, because I would > have said that I am doing the identical thing. Where is my thinking > wrong? As I have used the previous method before (with a different > model and controller) and it worked just fine. The reason that I used > the previous method was that I thought it was the correct way to do > it. > > Regards, > Paul > > 2008/7/8 David A. Black <dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org>:def self.get_region_code(region) find_by_region("#{region}") end is only different to just self.find_by_region(region) in the case that region is not a string, namely if it''s a nil, I imagine. Region.find_by_region(nil) will do the SQL query "SELECT * FROM regions where regions.region IS NULL" while Region.get_region_code(nil) will do the SQL query "SELECT * FROM regions WHERE regions.region = ''''" So I imagine that''s your difference. There is no region with region == '''', while there is one with region == nil ? -- 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 -~----------~----~----~----~------~----~------~--~---