Forum: Ruby on Rails can anyone help me with showing lists with conditions?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9440392ac04d3d76cd5b2f0641b679ca?d=identicon&s=25 Mike75 (Guest)
on 2009-05-24 12:15
(Received via mailing list)
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
39906837b290fe9354af90eba5e16ebb?d=identicon&s=25 Hemali Chauhan (Guest)
on 2009-05-24 21:00
(Received via mailing list)
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 Chauhan
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-24 22:09
(Received via mailing list)
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
http://guides.rubyonrails.org/getting_started.html

Colin

2009/5/24 Hemali Chauhan <hemali.chauhan5@gmail.com>
1c27ff0e8cf83f0de2078fd524150d04?d=identicon&s=25 pepijnlooije@gmail.com (Guest)
on 2009-05-25 10:51
(Received via mailing list)
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| %>
  <tr>
    <td><%=h post.client.name %></td>
    <td><%=h post.title %></td>
    [...]

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.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-05-25 12:28
(Received via mailing list)
On 25/05/2009, at 4:30 PM, pepijnlooije@gmail.com 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 . They may help.

Julian.

----------------------------------------------
Learn: http://sensei.zenunit.com/
Last updated 20-May-09 (Rails, Basic Unix)
Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
9440392ac04d3d76cd5b2f0641b679ca?d=identicon&s=25 Mike75 (Guest)
on 2009-05-25 13:50
(Received via mailing list)
hey.. Thanx Chauhan..
i got it working..
in my case, I used has_one with options..

thanx a bunch :)
9440392ac04d3d76cd5b2f0641b679ca?d=identicon&s=25 Mike75 (Guest)
on 2009-05-25 14:17
(Received via mailing list)
hey..  ( pepijnlooije@gmail.com )

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 <%@products.each 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ºÐ, "pepijnloo...@gmail.com" <pepijnloo...@gmail.com>
1c27ff0e8cf83f0de2078fd524150d04?d=identicon&s=25 pl (Guest)
on 2009-05-25 15:54
(Received via mailing list)
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| %>
  <tr>
    <td><%=h post.client.name %></td>
    <td><%=h post.title %></td>
    [...]

The show view (which works) is like this:

<p>
  <b>Name:</b>
  <%=h @post.client.name %>
</p>

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!
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-25 16:24
(Received via mailing list)
2009/5/25 pl <pepijnlooije@gmail.com>

>    [...]
>  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
This topic is locked and can not be replied to.