Find All records with associated records

22 views
Skip to first unread message

Naveed Alam

unread,
Apr 12, 2016, 3:18:26 AM4/12/16
to rubyonra...@googlegroups.com
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.

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

Colin Law

unread,
Apr 12, 2016, 3:45:44 AM4/12/16
to Ruby on Rails: Talk
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

Naveed Alam

unread,
Apr 12, 2016, 3:53:17 AM4/12/16
to rubyonra...@googlegroups.com
> 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

Colin Law

unread,
Apr 12, 2016, 5:53:40 AM4/12/16
to Ruby on Rails: Talk
On 12 April 2016 at 08:52, Naveed Alam <li...@ruby-forum.com> wrote:
>> 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

Naveed Alam

unread,
Apr 12, 2016, 9:44:17 AM4/12/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182787:
Sory none of them worked, pls chk my app,
https://www.dropbox.com/s/siioqa8q8xkdf46/tPOS.zip?dl=0

thanks.

Colin Law

unread,
Apr 12, 2016, 10:59:46 AM4/12/16
to Ruby on Rails: Talk
Just copy/paste the error here and the few lines of code around the failure.

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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f7a208acc016c2f5399a82b98c4bccab%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages