On 12 April 2016 at 08:17, Naveed Alam <li...@ruby-forum.com> wrote:
> 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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvj8f6i9CJbKhMSb-XqcEun%2Bi1_mHkfxpNV5FiUHztxHA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to