views:

31

answers:

5

Hi I have a block that I need to nest another block in. Or maybe I don't and I am doing this totally wrong. There is a UNIT and the UNIT has_many COMPANIES Then COMPANIES has_many USERS

on the UNIT show page I am trying to do something like this:

<% @company.each do |c|%>
 <%= c.name %>
  <% ??? each do |f| %>
   <p>
   Name: <%= f.name %>
      </p>
<% end %>
<% end %>

So basically for each Company I show the name of the Company, no problem there, but then I am trying to show that within each company there are particular Users I am listing that belong to that company. I can't define that in the controller because there are multiple companies.

That user is: cc = @user.find(:all, :conditions => ["position = ?", "Company Commander"])

I am not sure how to loop through for each Company and then for that Company loop through and list a particular user for that company.

Sorry if I did a poor job of explaining this. I am not sure if this is even the right way to be doing this.

Thanks in advance.

A: 

if a company has many users, then you should be able to just do 'company.users' to get the list of users associated with that company. then you can loop through or list those however you like.

Assuming you set up the relationship in your models, this is just how active record sets up these associations for you.

Pete
+2  A: 

Try this:

<% @company.each do |c| %>
  <%= h(c.name) %>
  <% c.users.each do |u| %>
    <p>Name: <%= h(u.name) %></p>
  <% end %>
<% end %>
John Topley
A: 

It seems you want to list the users with a position of 'Company Commander' associated with each company.

Maybe a named scope on user would help:

class User < ActiveRecord::Base
  ...
  named_scope :company_commanders, :conditions => "position = 'Company Commander'"
end

Now you can loop through them as you see fit.

<% @company.each do |c|%>
 <%= c.name %>
 <% c.users.company_commanders each do |f| %>
   <p>
     Name: <%= f.name %>
   </p>
 <% end %>
<% end %>
EmFi
Thanks the named_scope was a good idea. So now I have below (the if..else is to cover if I don't have a User with that position yet), but adding that scope causes nothing to show up. Any ideas why? <p> <% @company.each do |c| %> <%= h(c.name) %><br /> <% if c.users.company_commanders.nil? %> <%= link_to "Add this User", new_user_path %><br /> <% else %> <% c.users.company_commanders.each do |u| %> <p>Name: <%= h(u.name) %></p> <% end %> <% end %> <% end %> </p>
looloobs
Sorry that was not very helpful to put that in a comment, nonetheless as soon as I asked the question I solved it, I put it in the answer. Thanks again for reminding me of named_scopes.
looloobs
Use empty? instead of nil? If no results are found the named scope returns an empty list. Which is returns false on nil?.
EmFi
Thanks that is what I ended up doing, you have been very helpful appreciate it.
looloobs
A: 

Firstly, you need to have relations defined in your model:

# Unit model
has_many :companies
has_many :users, :through => :companies

# Company model
has_many :users
belongs_to :unit

...

Than in your Unit controller:

@unit = Unit.find(params[:id]) # or something similar

And in your view:

<% @unit.companies.each do |c|%>
 <%= c.name %>
  <% c.users each do |f| %>
   <p>
     Name: <%= f.name %>
   </p>
  <% end %>
<% end %>
klew
A: 

This is what ending up working:

named_scope :company_commanders, :conditions =>{:position => 'Company Commander'}

<% @company.each do |c| %>
  <%= h(c.name) %><br />
  <% if c.users.company_commanders.blank? %>
    <%= link_to "Add this User", new_user_path %><br />
  <% else %>
    <% c.users.company_commanders.each do |u| %>
        <p>Name: <%= h(u.name) %></p>
    <% end %>
  <% end %>
<% end %>
</p>
looloobs