Cs Webgrl
2009-Sep-28 19:53 UTC
Basic Rails problem with has_many nil values and permalinks
Hi, I have a model property with has_many. I find the property and then I need to find the name of the building or the neighborhood that the property belongs to. The property is not always assigned a building or neighborhood. This becomes a problem when createing permalinks with permalink_fu. has_permalink [:neighborhood_name, :building_name, :address] The neighborhood name and building name are defined here in property.rb def neighborhood_name neighborhood.name end def building_name building.name end If the property does not have a neighborhood or a building, then I get an error: The error occurred while evaluating nil.name): app/models/property.rb:425:in `neighborhood_name'' I know that I''m missing something really basic in Rails. Can someone help me out so that it generates the permalink even if the neighborhood or building are empty? Thanks. -- Posted via http://www.ruby-forum.com/.
Colin Law
2009-Sep-28 19:58 UTC
Re: Basic Rails problem with has_many nil values and permalinks
2009/9/28 Cs Webgrl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hi, > > I have a model property with has_many. I find the property and then I > need to find the name of the building or the neighborhood that the > property belongs to. The property is not always assigned a building or > neighborhood. > > This becomes a problem when createing permalinks with permalink_fu. > > has_permalink [:neighborhood_name, :building_name, :address] > > The neighborhood name and building name are defined here in property.rb > > def neighborhood_name > neighborhood.name > end > > def building_name > building.name > end > > If the property does not have a neighborhood or a building, then I get > an error: > The error occurred while evaluating nil.name): > app/models/property.rb:425:in `neighborhood_name'' > > > I know that I''m missing something really basic in Rails. Can someone > help me out so that it generates the permalink even if the neighborhood > or building are empty? >You could just test neighborhood and building in your _name methods and return an appropriate default if nil. Colin
Cs Webgrl
2009-Sep-28 20:07 UTC
Re: Basic Rails problem with has_many nil values and permalinks
Colin Law wrote:> You could just test neighborhood and building in your _name methods > and return an appropriate default if nil.Thanks for your reply. I don''t mean to make you do all of the work, but I can''t seem to find the correct syntax to go with this to make it pass correctly? Everything I put into the _name methods is ignored and I still get an error. Thanks for your help. -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Sep-28 20:30 UTC
Re: Basic Rails problem with has_many nil values and permal
Cs Webgrl wrote:> Colin Law wrote: > >> You could just test neighborhood and building in your _name methods >> and return an appropriate default if nil. > > > Thanks for your reply. I don''t mean to make you do all of the work, but > I can''t seem to find the correct syntax to go with this to make it pass > correctly? Everything I put into the _name methods is ignored and I > still get an error.What are you putting into those methods? Instead of asking for our code, show us yours.> > Thanks for your help.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Colin Law
2009-Sep-28 20:36 UTC
Re: Basic Rails problem with has_many nil values and permalinks
2009/9/28 Cs Webgrl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Colin Law wrote: > >> You could just test neighborhood and building in your _name methods >> and return an appropriate default if nil. > > > Thanks for your reply. I don''t mean to make you do all of the work, but > I can''t seem to find the correct syntax to go with this to make it pass > correctly? Everything I put into the _name methods is ignored and I > still get an error.You have snipped the original. I don''t know whether has_permalink is happy with nil parameters, if so then you could just do def neighborhood_name neighborhood.name if neighborhood end This will return nil if neighborhood is nil. If you need a default name however then something like def neighborhood_name neighborhood ? neighborhood.name : ''default_name'' end If Marnen reads this he will tell you (and probably me) in no uncertain terms to read up on basic Ruby coding :) Colin
Marnen Laibow-Koser
2009-Sep-28 20:48 UTC
Re: Basic Rails problem with has_many nil values and permal
Colin Law wrote:> 2009/9/28 Cs Webgrl <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> still get an error. > You have snipped the original. > I don''t know whether has_permalink is happy with nil parameters, if so > then you could just do > > def neighborhood_name > neighborhood.name if neighborhood > end > > This will return nil if neighborhood is nil. > If you need a default name however then something like > > def neighborhood_name > neighborhood ? neighborhood.name : ''default_name'' > endYeah, that''s about what I was thinking.> > If Marnen reads this he will tell you (and probably me) in no > uncertain terms to read up on basic Ruby coding :)Good God, am I becoming a List Personality [TM]? :) You''re right, I''d recommend that the OP brush up on basic Ruby. For you, I''d simply recommend not spoon-feeding...> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Colin Law
2009-Sep-28 21:06 UTC
Re: Basic Rails problem with has_many nil values and permal
2009/9/28 Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Colin Law wrote:> >> >> If Marnen reads this he will tell you (and probably me) in no >> uncertain terms to read up on basic Ruby coding :) > > Good God, am I becoming a List Personality [TM]? :) > > You''re right, I''d recommend that the OP brush up on basic Ruby. For you, > I''d simply recommend not spoon-feeding... >I think sometimes a modicum of spoon feeding is helpful to get people over immediate problems, this combined with hints as to where to go next. In this case I bounced the hint off you so I looked like the nice guy :) Somehow this thread seems to have got split, for me anyway. Your replies are appearing on a different thread to the original. Colin
Cs Webgrl
2009-Sep-28 21:27 UTC
Re: Basic Rails problem with has_many nil values and permal
Colin Law wrote:> > I think sometimes a modicum of spoon feeding is helpful to get people > over immediate problems, this combined with hints as to where to go > next. In this case I bounced the hint off you so I looked like the > nice guy :)Thank you for the information. Yes, I need to read up on some basic Ruby. I was thinking that I needed to do neighborhood.<something> and not just neighborhood which is why I was unsuccessful. Back to reading. I have the David Black''s Ruby for Rails book along with the Pick axe book and AWD. If there''s another resource that you recommend, please let me know. -- Posted via http://www.ruby-forum.com/.