New to Rails... Help!


#1

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

<%= people.name %> <%= people.country.location %> // This works

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

<% @countries.each do |country| %>

<%= link_to country.location, :action => "show", :id => country.id %>

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

<%=h @country.population %> // this works <%=h @country.tribe %> // 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


#2

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


#3

class Country

def people_tribes
peoples.map{|p|p.tribe}.join(’,’)
end
end


#4

Agile Web D. with Rails is the standard for getting up and
running… also I like Practical Rest with Rails


#5

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


#6

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


#7

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