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.
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.
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.
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.
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.