# Basic Rails problem with has\_many nil values and permalinks

**URL:** <https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304>\
**Category:** Rails\
**Created:** [September 28, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304 "2009-09-28T21:53:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/1 "2009-09-28T21:53:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/2 "2009-09-28T22:00:37Z")

</div>

2009/9/28 Cs Webgrl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 10:07pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/3 "2009-09-28T22:07:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/4 "2009-09-28T22:30:46Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 10:48pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/5 "2009-09-28T22:48:36Z")

</div>

Colin L. wrote:

> 2009/9/28 Cs Webgrl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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 🙂

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…

> Colin

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/6 "2009-09-28T23:08:31Z")

</div>

2009/9/28 Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/7 "2009-09-28T23:27:14Z")

</div>

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 🙂

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.

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [September 28, 2009, 10:38pm UTC](https://www.ruby-forum.com/t/basic-rails-problem-with-has-many-nil-values-and-permalinks/176304/8 "2009-09-28T22:38:05Z")

</div>

2009/9/28 Cs Webgrl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
