Answer the question
In order to leave comments, you need to log in
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
scope :with_images, self.images.count > 0
Answer the question
In order to leave comments, you need to log in
scope :with_images {:joins => [:images], :conditions => "images.project_id IS NOT NULL"}
def self.with_images
all.reject{|t| t.images.empty?}
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question