Find All records with associated records

Hi,

Using rails 4, I have two models Supplier and Category

Category belongs_to Supplier

and Supplier has_many categories

now the suppliers table has fields, company_name, address, phone etc. and id

and the categories has cat_name and supplier_id

in the categories index view I want to display all categories names along with associated company_names.

in the categories_controller my index method is below.

def index     @categories = Category.all #??? what to do to find company_name   end

and my index.html.erb view is

<% @categories.each do |category| %>       <tr>         <td><%= category.cat_name %></td>         <td><%= category.cat_sub_name %></td>         <td><%= category.supplier %></td>         <td><%= link_to 'Show', category %></td>         <td><%= link_to 'Edit', edit_category_path(category) %></td>         <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>       </tr>     <% end %>

I have no idea how to this. pls help.

Hi,

Using rails 4, I have two models Supplier and Category

Category belongs_to Supplier

and Supplier has_many categories

now the suppliers table has fields, company_name, address, phone etc. and id

and the categories has cat_name and supplier_id

in the categories index view I want to display all categories names along with associated company_names.

in the categories_controller my index method is below.

def index     @categories = Category.all #??? what to do to find company_name

See below

  end

and my index.html.erb view is

<% @categories.each do |category| %>       <tr>         <td><%= category.cat_name %></td>         <td><%= category.cat_sub_name %></td>         <td><%= category.supplier %></td>

category.supplier is the whole supplier record, so if you want the name it is just category.supplier.company_name

Such is the magic of Rails.

As a beginner I suggest you work right through a good tutorial in order to get the basics or Rails. The one I suggest is railstutorial.org (which is free to use online).

Colin

As a beginner I suggest you work right through a good tutorial in order to get the basics or Rails. The one I suggest is railstutorial.org (which is free to use online).

Colin

Thanks colin I already tried this but it give me the error:

undefined method `company_name' for nil:NilClass

Please quote the previous message when posting, this is a mailing list not a forum (though you may be accessing it via a forum like interface).

If the statement category.supplier.company_name gives the error undefined method `company_name' for nil:NilClass then that means that category.supplier is nil, or to put it another way, it means that category does not have an associated supplier. You probably need something like <td><%= category.supplier.company_name if category.supplier %></td> which will only attempt to determine the name if category.supplier is not nil

Colin

Colin Law wrote in post #1182787:

Colin Law wrote in post #1182787:

As a beginner I suggest you work right through a good tutorial in order to get the basics or Rails. The one I suggest is railstutorial.org (which is free to use online).

Colin

Thanks colin I already tried this but it give me the error:

undefined method `company_name' for nil:NilClass

Please quote the previous message when posting, this is a mailing list not a forum (though you may be accessing it via a forum like interface).

If the statement category.supplier.company_name gives the error undefined method `company_name' for nil:NilClass then that means that category.supplier is nil, or to put it another way, it means that category does not have an associated supplier. You probably need something like <td><%= category.supplier.company_name if category.supplier %></td> which will only attempt to determine the name if category.supplier is not nil

Colin

Sory none of them worked, pls chk my app,

Just copy/paste the error here and the few lines of code around the failure.

Colin