2013-07-15 75 views
0

我有一個名爲「Project」的模型和一個名爲published的類方法?我決定是否發佈一個項目。我想創建一個基於這個類方法的範圍。什麼是正確的語法?對範圍使用類方法

這是我的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)

回答

0

我想避免創建一個列我的項目模型,但我crea特德一個叫做發表,只是基於公佈的價值來設定它的價值?方法。然後我相應地更新列並將其用於範圍。