Ruby on Rails Monday, April 29, 2013

On 29 April 2013 23:39, akkdio <akkdio@gmail.com> wrote:
> am a beginner and I appreciate an answer to help me understand where my
> knowledge gap is:
>
> The app is to display a post. The posts belong to a category (appetizers,
> entrees...) My thought was to use scopes to display all the appetizers on
> the posts in one view and then have the entrees in another view and so on.
>
> The models:
>
> class Post < ActiveRecord::Base
> attr_accessible :body, :category_id, :title
>
> belongs_to :category
>
> scope :appetizers, -> { where(post.category.name => "Appetizers")}
>
> end
>
>
> class Category < ActiveRecord::Base
> attr_accessible :name
>
> has_many :posts
> end
>
> In the view I want to loop through the posts where the category name is
> "Appetizers".
>
> <table>
> <tr>
> <th>Title</th>
> <th>Body</th>
> <th>Category</th>
> </tr>
>
> <% @post.appetizers.each do |app| %>
> <tr>
> <td><%= app.title %></td>
> <td><%= app.body %></td>
> <td><%= app.category.name%></td>

As tamouse suggests it would have been better if you could have told
us the exact error and which line it is on, but if the error is on
this line, and is saying calling method name for nil, then it is
because one of the posts does not have an associated category (so
app.category is nil)

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+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment