Ruby(1.8.7)on rails(2.0.1) - 调用模型符号时出现NoMethodError

时间:2013-04-02 17:11:56

标签: ruby-on-rails

当我尝试访问模型NewsItem.published中的符号时,我收到NoMethodError。

调用的控制器是show:

class NewsItemsController < ApplicationController
  def index
    @news_items = NewsItem.published
  end

  def show
    @news_item = NewsItem.find(params[:id])
  end

end

错误是:

undefined method `published' for #<Class:0x105ca3fe0>

vendor/rails/activerecord/lib/active_record/base.rb:1541:in `method_missing_without_paginate'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:93:in `method_missing'
app/controllers/news_items_controller.rb:3:in `index'

型号代码为:

class NewsItem < ActiveRecord::Base
  belongs_to :news_category
  validates_presence_of :title, :summary, :body, :release_at

  scope :published, :conditions => ["news_items.release_at > ?", Time.now]
  scope :homepage_published, :conditions => ["news_items.release_at > ?", Time.now], :limit => 3
end

我已经合并了另一个开发人员代码,所以我想知道是否有一些方法声明丢失......

任何帮助或建议将不胜感激。感谢。

编辑 - 将lambda添加到模型代码:

class NewsItem < ActiveRecord::Base
  belongs_to :news_category
  validates_presence_of :title, :summary, :body, :release_at


  scope :published, lambda { 
    where(["news_items.release_at > ?", Time.now])
  }  
  scope :homepage_published, lambda { 
    where(["news_items.release_at > ?", Time.now])
  }  
  #scope :published, :conditions => ["news_items.release_at > ?", Time.now]
  #scope :homepage_published, :conditions => ["news_items.release_at > ?", Time.now], :limit => 3
end

也可能值得一提的是,这个应用程序受版本控制,在合并了添加了新闻项代码的开发人员的更改后,我跑了:

bundle exec rake db:migrate

1 个答案:

答案 0 :(得分:1)

使用此:

scope :published, lambda { 
  where(["news_items.release_at > ?", Time.now])
}