Skip to content

Instantly share code, notes, and snippets.

@rishighan
Last active August 29, 2015 14:05
Show Gist options
  • Save rishighan/6c0775aea7fbc525ae40 to your computer and use it in GitHub Desktop.
Save rishighan/6c0775aea7fbc525ae40 to your computer and use it in GitHub Desktop.
How to check for existence of element in a collection
@log.last.categories
=> #<ActiveRecord::Associations::CollectionProxy [#<Category id: 1, title: "General", description: "Toto", created_at: "2014-07-25 19:09:17", updated_at: "2014-07-30 14:50:42">, #<Category id: 6, title: "Highlight", description: "Some shenanigans on the way to the graveyard.", created_at: "2014-08-12 15:56:42", updated_at: "2014-08-12 15:56:42">]>
@log = Post.is_draft("no").group_by_category("exclude", ["Projects", "Hero", "Feature"]).page(params[:page]).per(10)
@rishighan
Copy link
Author

I want to check the existence of "Highlight" in the collection, and then based on the presence display a certain style in the view.

So,
I want to be able to do something like:

@log.each do |log|
  log.categories.each do |cat|
    # if cat.title == "Highlight"
    # render a specific style
    #else
    # render the normal style
end 
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment