# Can anyone help me with showing lists with conditions?

**URL:** <https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690>\
**Category:** Rails\
**Created:** [May 24, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690 "2009-05-24T12:15:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 24, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/1 "2009-05-24T12:15:58Z")

</div>

hi  
i’m trying things out with rails, and I’m having trouble with showing  
a list of products.  
i hope someone can help me out with this…

my tables are simple and it contains the following fields

product table : id | Name | Price | Country\_id  
country table : id | CountryName

and I input products info with selected id numbers from the “country”  
table and insert the country.id into products.Country\_id.

I am trying to show countryName from the country table on the products  
list.

it would be wonderful if anyone can help me out with this…

thank you

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 24, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/2 "2009-05-24T21:00:31Z")

</div>

Hi,

You must have defined has\_many and belongs\_to association for Country  
and Product.

So now you can directly write:  
@product.country.name

-Hemali C.

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 24, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/3 "2009-05-24T22:09:45Z")

</div>

Also you will make life much easier for yourself if you stick to the  
rails  
naming conventions, so the column names will be id, name, price,  
country\_id  
and id, country\_name.

You might find this helpful

> **[Getting Started with Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/getting_started.html)**
>
> Getting Started with RailsThis guide covers getting up and running with Ruby on Rails.After reading this guide, you will know: How to install Rails, create a new Rails application, and connect your application to a database. The general layout of a...

Colin

2009/5/24 Hemali C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 25, 2009, 10:51am UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/4 "2009-05-25T10:51:19Z")

</div>

Hi, I’m also new to rails and I’m trying the same thing. I’m extending  
the Getting Started example to learn.

I’ve made an extra table called `clients`: (id | name) and I already  
had the table `posts`: (id | title | content | created\_at | updated\_at  
| client\_id). The index view for posts loops through all the posts I  
want to show the client name. I believe the way to do this is like  
this:

\<% @posts.each do |post| %\>

 \<%=h post.client.name %\> \<%=h post.title %\> [...]

I have set up all the assocations (posts belongs\_to client, client  
has\_many posts) but it doesn’t work. I’m probably making a simple  
mistake but hey, I’m new 🙂

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 25, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/5 "2009-05-25T12:28:06Z")

</div>

On 25/05/2009, at 4:30 PM, removed\_email\_address@domain.invalid wrote:

> \<% @posts.each do |post| %\>

How does it fail? What error messages does it give?  
Please send us your full model code

When asking for help, it’s best to give as much context as possible.

Check out my free videos at [http://sensei.zenunit.com](http://sensei.zenunit.com) . They may help.

Julian.

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 25, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/6 "2009-05-25T13:50:38Z")

</div>

hey… Thanx Chauhan…  
i got it working…  
in my case, I used has\_one with options…

thanx a bunch 🙂

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 25, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/7 "2009-05-25T15:54:36Z")

</div>

This is pretty weird actually, in the show view the name is displayed  
(nice!!!), but in the index view (the list of all the posts) it isn’t  
working yet. The code for the index view (not working) is as I wrote  
earlier:

\<% @posts.each do |post| %\>

 \<%=h post.client.name %\> \<%=h post.title %\> [...]

The show view (which works) is like this:

**Name:** \<%=h @post.client.name %\>

The error generated by the index view:

Showing app/views/posts/index.html.erb where line #12 raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.name

Good job Mike that you got it working 🙂 It’s pretty hard for me  
starting with Rails, coming from a PHP/CodeIgniter background. The  
convention over configuration is really great though! I also watched  
some screencasts last night, very useful!

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 25, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/8 "2009-05-25T14:17:04Z")

</div>

hey… ( removed\_email\_address@domain.invalid )

i hope you got it working by now… 🙂  
anyway, I’ve got my problem solved like this.

i wrote

belongs\_to :Country, :foreign\_key =\> :Country\_id  
in products.rb … it means that the each products belongs to the  
Country table  
with the foreign\_key of Country\_id that matches with id field of the  
Country table.  
well, I got it mixed up with has\_one and the belongs\_to… ^^  
anyway… then, in the view.html,erb file, i wrote  
\<%=h  
product.Country.CountryName %\>  
inside the \<removed\_email\_address@domain.invalid do… loop… %\>  
and i got it working.  
I hope this makes sense to to you… I’m new too… so I’m not sure if  
I’m on the right track 🙂

well I hope you get yours working with this, or with the better  
solution

good luck to both of us… 🙂

On 5¿ù25ÀÏ, ¿ÀÈÄ3½Ã30ºÐ, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [May 25, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/can-anyone-help-me-with-showing-lists-with-conditions/168690/9 "2009-05-25T16:24:40Z")

</div>

2009/5/25 pl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> […]  
> Showing app/views/posts/index.html.erb where line #12 raised:
> 
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.name

This means that post.client is nil (hence error with post.client.name).  
Are  
you sure that all your posts have a client? You could use  
\<%= h post.client.name if post.client %\>  
which will at least let it show posts that have clients and show nothing  
for  
those that have not, while you work out why some (or all) not.

Colin
