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.

2009/9/28 Cs Webgrl [email protected]:

has_permalink [:neighborhood_name, :building_name, :address]

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

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

Cs Webgrl wrote:

Colin L. 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
[email protected]

Colin L. wrote:

2009/9/28 Cs Webgrl [email protected]:

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

Yeah, 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 :slight_smile:

Good God, am I becoming a List Personality [TM]? :slight_smile:

You’re right, I’d recommend that the OP brush up on basic Ruby. For you,
I’d simply recommend not spoon-feeding…

Colin

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

2009/9/28 Marnen Laibow-Koser [email protected]:

Colin L. wrote:

If Marnen reads this he will tell you (and probably me) in no
uncertain terms to read up on basic Ruby coding :slight_smile:

Good God, am I becoming a List Personality [TM]? :slight_smile:

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 :slight_smile:

Somehow this thread seems to have got split, for me anyway. Your
replies are appearing on a different thread to the original.

Colin

Colin L. 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 :slight_smile:

Thank you for the information. Yes, I need to read up on some basic
Ruby. I was thinking that I needed to do neighborhood. 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.

2009/9/28 Cs Webgrl [email protected]:

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 :slight_smile:

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs