Passing hash from controller to view and pluralization?

hi,
i have 2 tables (counties and towns). counties has_many towns and towns
belong_to counties.

now my question:

i thought i would need to do is say @counties = Counties.find(:all).
should that not return to me all counties in the counties table WITH all
towns associated with each county?

in my view i was getting error when doing this

if(counties.has_towns?)

saying undefined has_towns methods…

another question about plurulization. if i were to say @county vs
@counties, is there a difference? would RoR assume i just want
information for 1 county? or does RoR treat it like the same variable?

baker1 wrote:

in my view i was getting error when doing this

if(counties.has_towns?)

saying undefined has_towns methods…

If you have “has_many :towns” in your model definition, than “towns” is
the methods you use to access the association.

@county.towns #=> an array of Town objects

If you want to know if this county has any associated towns, remember
that it returns an array. So you can use any array methods on it.

if @county.towns.any?
do_stuff
end

another question about plurulization. if i were to say @county vs
@counties, is there a difference? would RoR assume i just want
information for 1 county? or does RoR treat it like the same variable?

It’s entirely up to you. Rails doesn’t care what you name your
variables.

def index
@foo = Something.find(1)
@bar = Domething.find(:all)
end

Now from index.rhtml @foo is a single Something object, and @bar is an
array of Something objects. However convention tell us it’s best to
names arrays of things plural and single object singular.

hello alex, i really appreciate your help!

if @county.towns contains the towns for the particular county, how can i
now increment to the next county and show the next set of towns?

basically id like to display something like this

for county in @counties
    print county name

    for towns in @counties
        print town name
    end
end

is this possible or do i need more than one data container passed to the
view?

Alex W. wrote:

baker1 wrote:

in my view i was getting error when doing this

if(counties.has_towns?)

saying undefined has_towns methods…

If you have “has_many :towns” in your model definition, than “towns” is
the methods you use to access the association.

@county.towns #=> an array of Town objects

If you want to know if this county has any associated towns, remember
that it returns an array. So you can use any array methods on it.

if @county.towns.any?
do_stuff
end

another question about plurulization. if i were to say @county vs
@counties, is there a difference? would RoR assume i just want
information for 1 county? or does RoR treat it like the same variable?

It’s entirely up to you. Rails doesn’t care what you name your
variables.

def index
@foo = Something.find(1)
@bar = Domething.find(:all)
end

Now from index.rhtml @foo is a single Something object, and @bar is an
array of Something objects. However convention tell us it’s best to
names arrays of things plural and single object singular.

hey Alex thanks, i will try this when i get home today.

coming from a c++ perspective, i am still having difficulty
understanding how ruby and rails work. its like what are big no-no’s in
c++ is a.o.k. in ruby!

a few more questions if you dont mind:

now @counties is the object i declared in my controller that contains
everything that my mySql counties table has? what is singular ‘county’
in the for loop? is that the same as saying

for(int i = 0; i < number of counties in counties table; i++)
{
do stuff to each county etc…?

}

Alex W. wrote:

baker1 wrote:

hello alex, i really appreciate your help!

if @county.towns contains the towns for the particular county, how can i
now increment to the next county and show the next set of towns?

basically id like to display something like this

for county in @counties
    print county name

    for towns in @counties
        print town name
    end
end

is this possible or do i need more than one data container passed to the
view?

you almost have it

<% for county in @counties %>

<%= county.name %>

<% for town in county.towns %>

<%= town.name %>

<% end %> <% end %>

Also, your controller should be

@counties = Counties.find(:all)

Then your view should have

<% @counties.each do |county| %>
<% if county.towns.any? %>
<%= link_to county.name, :action => ‘show’, :id => county %>

<% end %>
<% end %>

Or something similar.

Note that in your example you cant call any model specific methods on
@counties because it is an array of County objects, not a county object
itself.

Hi,

Assuming that in your view the only hash you receive is @counties we can
iterate its members this way:

for county in @counties
    print county.name

    for town in county.towns
        print town.name
    end
end

–Romeu

-----Mensagem original-----
De: [email protected]
[mailto:[email protected]] Em nome de baker1
Enviada em: quinta-feira, 10 de agosto de 2006 17:22
Para: [email protected]
Assunto: [Rails] Re: passing hash from controller to view and
pluralization?

hello alex, i really appreciate your help!

if @county.towns contains the towns for the particular county, how can i
now increment to the next county and show the next set of towns?

basically id like to display something like this

for county in @counties
    print county name

    for towns in @counties
        print town name
    end
end

is this possible or do i need more than one data container passed to the
view?

Alex W. wrote:

@county.towns #=> an array of Town objects

information for 1 county? or does RoR treat it like the same variable?
array of Something objects. However convention tell us it’s best to
names arrays of things plural and single object singular.


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.8/415 - Release Date: 9/8/2006


No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.9/416 - Release Date:
10/8/2006

baker1 wrote:

hello alex, i really appreciate your help!

if @county.towns contains the towns for the particular county, how can i
now increment to the next county and show the next set of towns?

basically id like to display something like this

for county in @counties
    print county name

    for towns in @counties
        print town name
    end
end

is this possible or do i need more than one data container passed to the
view?

you almost have it

<% for county in @counties %>

<%= county.name %>

<% for town in county.towns %>

<%= town.name %>

<% end %> <% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs