为什么我的模型中存在属性的NoMethodError?

时间:2010-09-17 14:46:35

标签: ruby-on-rails methods rake

这是我得到的错误:

ContactPostalcardsController#skip (NoMethodError) "undefined method `status=' for #<ContactPostalcard:0x2b21433d64b0>"

这是调用它的代码,并尝试为ContactPostalcard(模型)的status属性赋值:

  def skip

    @contact_postalcard = ContactPostalcard.new(params[:contact_postalcard])
    @contact_postalcard.contact_id = params[:contact_id]
    @contact_postalcard.postalcard_id = params[:postalcard_id]

    @contact_postalcard.status = "skipped"
    @contact_postalcard.date_sent = Date.today
    @contact_postalcard.date_created = Date.today

    if @contact_postalcard.save
      render :text => 'This email was skipped!'

    end
  end 

这是推荐的模型。请注意,“annotate”输出将状态显示为属性:

class ContactPostalcard < ActiveRecord::Base
  attr_accessible :title, :contact_id, :postal_id, :postalcard_id, :message, :campaign_id, :date_sent, :status


  belongs_to :contact
  belongs_to :postalcard

  alias_attribute :body, :message
  alias_attribute :subject, :title 

  named_scope :nosugar, :conditions => { :sugarcrm => false }

  def company_name
    contact = Contact.find_by_id(self.contact_id)

    return contact.company_name

  end 

  def asset
    Postalcard.find_by_id(self.postalcard_id)
  end

  def asset_class
    Postalcard.find_by_id(self.postalcard_id).class.name
  end  

end



# == Schema Information
#
# Table name: contact_postalcards
#
#  id            :integer         not null, primary key
#  title         :string(255)
#  contact_id    :integer
#  postalcard_id :integer
#  message       :text
#  campaign_id   :integer
#  date_sent     :datetime
#  created_at    :datetime
#  updated_at    :datetime
#  postal_id     :integer
#  sugarcrm      :boolean         default(FALSE)
#  status        :string(255)
#

我不清楚为什么我会继续得到'未定义的方法' - 我已经添加了status属性(之前已经丢失但是使用了迁移然后进行了raked),所以需要一些帮助......谢谢。

1 个答案:

答案 0 :(得分:1)

您是否在运行迁移后重新启动了Rails应用程序?如果您正在生产模式下运行,Rails会缓存您的类,直到您重新启动它,并且由于状态在迁移之前不是属性,因此Rails不会为其添加访问器方法,这可以解释为什么status=未定义。