A
A
Anton Ipatov2014-01-14 14:28:04
Ruby on Rails
Anton Ipatov, 2014-01-14 14:28:04

How can I add a scope so that only those projects that have images are displayed in the index?

How can I add a scope so that only those projects that have images are displayed in the index?

class Project < ActiveRecord::Base
  attr_accessible :description, :title, :year, :location

  belongs_to :user

  has_attachments :images, accept: [:jpg, :png, :gif]

  validates :user_id, presence: true
  validates :title, presence: true

  default_scope order: 'projects.created_at DESC'
end

I understand what I should add, but it does not work
scope :with_images, self.images.count > 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arkady Butermanov, 2014-01-14
@Arkadey

scope :with_images {:joins => [:images], :conditions => "images.project_id IS NOT NULL"}

But better without scopes, IMHO. It's not pretty. If I were you, I would just file a method of some type:
def self.with_images
 all.reject{|t| t.images.empty?}
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question