Forum: Ruby on Rails New to Rails... Help!

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.
Jay M. (Guest)
on 2008-12-01 21:05
I am new to Ruby/InstantRails. I am using InstantRails 2.0.2 with mysql
database

My application is named Area, it has two tables: people and country
Table people has two columns: name and tribe
Table country has two columns: location and population

In the models I have:
class People < ActiveRecord::Base
belongs_to :country
end
class Country < ActiveRecord::Base
   has_many :peoples
end

I have country_id in the people's table.

I know I can do this:
<% @peoples.each do |people| %>
<td><%= people.name %></td>
<td><%= people.country.location %></td> // This works

What I want to do is to display location on country\index.html.erb

<% @countries.each do |country| %>
<td><%= link_to country.location, :action => "show", :id => country.id
%></td>


Then when I click on 'location' as a link, I want to display the 'tribe'
along side with 'population' for that 'location' on
view\country\show.html

<td><%=h @country.population %></td>   // this works

<td><%=h @country.tribe %></td>   // this does not works

My question is, how can I reference a column from the people's table
with a country's object? I know it works the other round.

Please help!
Cypray
wfisk (Guest)
on 2008-12-01 21:52
(Received via mailing list)
I am sure there's a few ways of doing this.  Here's an easy one:
declare a method in  the Country class and use that.

class Country
  ....

  def people_tribes
    peoples.map{|p|p.tribes}.join(',')
  end
end

now in your view use country.people_tribes

William
wfisk (Guest)
on 2008-12-01 21:54
(Received via mailing list)
class Country
  ....

  def people_tribes
    peoples.map{|p|p.tribe}.join(',')
  end
end
Jay M. (Guest)
on 2008-12-02 02:44
William, thanks for your help in resolving this issue.
It works great now. Thanks a lot.

Please, if you know any good book or online material
on Ruby/InstantRails 2.0.2, please post it in the Forum.
Thanks
Cypray










wfisk wrote:
> class Country
>   ....
>
>   def people_tribes
>     peoples.map{|p|p.tribe}.join(',')
>   end
> end
Nellboy (Guest)
on 2008-12-02 13:29
(Received via mailing list)
Agile Web D. with Rails is the standard for getting up and
running... also I like Practical Rest with Rails
Sazima (Guest)
on 2008-12-02 16:56
(Received via mailing list)
Jay,

You're confusing Rails with InstantRails. Rails is the web framework,
Instant Rails is a package containing Rails + web server + DB, etc.

Google for Rails books, there are also plenty of suggestions in other
topics in this group. Finally, if you have the time, I would start by
1st learning Ruby and then moving to Rails with AWDR, The Rails Way,
etc.

Cheers, Sazima
Jay M. (Guest)
on 2008-12-02 20:01
Sazima,

You are so right Sazima. I am confused like a beginner. Thanks for
clearing that up for me. I have started learning Ruby. Thanks for all
the suggestions guys.

Jay.



Sazima wrote:
> Jay,
>
> You're confusing Rails with InstantRails. Rails is the web framework,
> Instant Rails is a package containing Rails + web server + DB, etc.
>
> Google for Rails books, there are also plenty of suggestions in other
> topics in this group. Finally, if you have the time, I would start by
> 1st learning Ruby and then moving to Rails with AWDR, The Rails Way,
> etc.
>
> Cheers, Sazima
This topic is locked and can not be replied to.