我有一个名为“项目”的模型和一个名为已发布的类方法?我在哪里确定项目是否已发布。我想基于此类方法创建一个范围。什么是正确的语法?
这就是我的 Project.rb 的样子:
class Project < ActiveRecord::Base
attr_accessible :title, :images_attributes, :ancestry, :user_id, :built, :remix
def published?
published = false
if remix_id.blank?
# check if the remix has been updated
if updated_at != created_at
published = true
end
else
# for non remixed projects, projects are published if the title has been updated and a picture has been uploaded
if title.starts_with("Untitled")
if images.count > 0
published = true
end
end
end
end
return published
end
我试过了:
scope :published, where(published? => true)
scope :published, where(:published? => true)