On Tuesday, April 30, 2013 12:45:54 PM UTC+1, akkdio wrote:
>
> So this works but it seems like it is not the best way to do it - seems
> like the controller would get bloated if there are a lot of categories.
> Another issue is that in the scope I have to use category_id => 3 to get
> the appetizers to list. I would like at least to have "post.category.name=>
> "Appetizers" so to be more clear about what is being scoped but this
> give me an error:
undefined method `key?' for nil:NilClass
>
Rails doesn't understand that post.category.name in the where clause (not
in relation to a specific post) means to add a condition on the category's
name. You'd have to do something like
scope :appetizers, -> {joins(:category).where(:categories => {:name =>
'Appetizers'})}
> I have a feeling there are many ways to display the appetizer listing in
> the view. Adding actions to the controller and adding routes seems not
> elegant. Can you suggest an alternative and or point to the concept I seem
> to be missing in understanding how to display a subset of information from
> the database in the view. I would like to use scopes and perhaps a method
> in the model...
>
I'd suggest adding a single route/action for displaying the posts from any
category. You can still make it look pretty by setting up slugs for
categories and fetching categories by slug rather than id (friendly_id is
one of many gems that do this).
You might also consider doing something like this, instead of using scopes
(assuming you have this by category action)
@posts = Category.find(params[:category_id]).posts
Fred
> Again, sorry for not posting the error, I appreciate any comments you can
> give on the subject.
>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/UP46FpLAm94J.
For more options, visit https://groups.google.com/groups/opt_out.