0

我有一个名为“项目”的模型和一个名为已发布的类方法?我在哪里确定项目是否已发布。我想基于此类方法创建一个范围。什么是正确的语法?

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

4

1 回答 1

0

我想避免为我的项目模型创建一个列,但我创建了一个名为已发布的列,并且只是根据发布的?方法。然后我相应地更新该列并将其用于范围。

于 2013-07-15T16:23:02.407 回答