【问题标题】:Can't get a non-mass-assignment value to set or save无法获取要设置或保存的非质量分配值
【发布时间】:2012-03-15 15:04:46
【问题描述】:

这里令人沮丧的简单问题。我正在使用 Rails 3.1 并具有以下类:

class Job < ActiveRecord::Base
  attr_accessor :active
  attr_accessible :roleTagline, :projectTagline, :projectStartDate, :projectDuration, :postedStartDate,
    :postedEndDate, :skillsRequired, :skillsPro, :experiencedRequired, :description, :active
  scope :is_active, :conditions => {:active => 1}

  validates :roleTagline,  :presence => true,
                :length => { :minimum => 5 }
  validates :projectTagline, :presence => true,
                :length => { :minimum => 5 }
  belongs_to :job_provider

#   def active=(act)
#       @active = act
#   end
end

在我的控制器中,我尝试使用批量分配(ActiveRecord 构建助手之一)创建作业,然后将“活动”属性设置为 1,然后保存作业。这是控制器代码:

def create
  @job_provider = current_user.job_provider
  @job = @job_provider.jobs.build(params[:job])
  @job.active= 1 # debug logging @job.active here gives an empty string
  if @job.save # etc.

我已经尝试了删除 attr_accessor 并改为编写自己的 setter 的所有组合,但无论我做什么,我似乎都找不到正确的组合来让属性留在模型上。我不认为它不是活动记录,因为即使在 @job.save 之前,该属性就消失了(来自使用调试日志记录)。我用谷歌搜索,但无法弄清楚我做错了什么。有人可以帮忙吗?

编辑:来自 rake 的 schema.rb:

create_table "jobs", :force => true do |t|
t.string   "roleTagline"
t.string   "projectTagline"
t.date     "projectStartDate"
t.integer  "projectDuration"
t.date     "postedStartDate"
t.date     "postedEndDate"
t.string   "skillsRequired"
t.string   "skillsPro"
t.string   "experiencedRequired"
t.string   "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer  "active"
t.integer  "job_provider_id"
end

另一个编辑:

好吧,经过更多谷歌搜索后,我终于在这里找到了答案:

How can I override the attribute assignment in an active record object?

如果您修改的是 ActiveRecord 属性而不是类实例,您需要这样做:

self[:fieldname] = 值

【问题讨论】:

  • “@job.save 属性消失”是什么意思?另外,您能否发布您的作业表的架构,因为它是我们唯一需要复制您的示例的东西?
  • 我尝试使用 to_yaml 函数并使用 @job.active 访问对象进行 debug.log,结果和插入的 SQL 都显示属性未设置。
  • 现在正在尝试获取数据库方案。 “活动”列是一个整数。
  • 这是来自 Rake 的 schema.rb:create_table "jobs", :force => true do |t| t.string "roleTagline" t.string "projectTagline" t.date "projectStartDate" t.integer "projectDuration" t.date "postedStartDate" t.date "postedEndDate" t.string "skillsRequired" t.string "skillsPro" t. string "experiencedRequired" t.string "description" t.datetime "created_at" t.datetime "updated_at" t.integer "active" t.integer "job_provider_id" end

标签: ruby-on-rails ruby activerecord


【解决方案1】:

从模型中删除 attr_accessor :active。它导致值被保存在实例变量中,而不是通过attributes 哈希值保存到数据库中。您不必编写访问器,因为 ActiveRecord 会根据 active 列自动为您完成。

它在调试日志中显示为空白,因为它被初始化为 nil。如果删除 attr_accessor 行并将数据库中的 active 列更改为 NOT NULL DEFAULT 0,它将被初始化为 0。即在 ActiveRecord 迁移或 schema.rb 中为 :null =&gt; false, :default =&gt; 0

【讨论】:

  • 这就是答案,here 是一个要点,表明如果您只删除 attr_accessor :active,那么它将按预期工作。
【解决方案2】:

我认为您没有将 job_provider_id 添加到 att_accessible。试试下面的

attr_accessible :roleTagline, :projectTagline, :projectStartDate, :projectDuration, :postedStartDate, :postedEndDate, :skillsRequired, :skillsPro, :experiencedRequired, :description, :active, :job_provider_id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多