In my articles model, I created a scope that links categories to their
respective articles posts. When I use the rails console, the method
works perfectly when I query the db.

article.rb

scope :category_technology, -> {where(category_id: 1)}

@articles.category_technology
Article Load (0.5ms)  SELECT "articles".* FROM "articles" WHERE
"articles"."category_id" = ?  ORDER BY "articles"."created_at" DESC
[["category_id", 1]]

Which works.
_________________________________________________________________________

The problem is that I'm unable to use it in the view and iterate over
the records with category_id: 1.

What I'd like to do is something like this
technology_controller.rb

def index
@technology_articles = Article.category_technology.all.limit(10).last
end

_____________________________________________________________________

In the index.html.erb

<% if @technology_articles.present? %>
<% @technology_articles.each do |article| %>
<- omitted ->
<% end %>

I'm getting undefined method `each' for #<Article:0x8300868>

Can you help?

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

-- 
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/498cef49f70c064c529c31e0ebe8ae2a%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to