【发布时间】:2015-06-21 08:41:26
【问题描述】:
我有两个模型,Job 和 Survey。我正在使用 PaperTrail gem 来跟踪模型上发生的更改,并且我想在每次创建时将 job_id 保存到版本中。
# app/models/job.rb
class Job < ActiveRecord::Base
has_paper_trail :ignore => [:id, :created_at, :updated_at],
:meta => { :resource_id => self.id }
has_one :survey
end
# app/models/survey.rb
class Survey < ActiveRecord::Base
has_paper_trail :ignore => [:id, :created_at, :updated_at],
:meta => { :resource_id => self.job_id }
belongs_to :job
end
# app/db/schema.db
# versions
create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.text "object_changes"
t.integer "resource_id"
end
# surveys
create_table "surveys", force: :cascade do |t|
t.string "survey"
t.integer "job_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
当我运行此代码并尝试保存新记录时,我收到以下错误:
undefined method `job_id' for #<Class:0x007fd53d70c0a0>
如何从我的 Survey 模型中正确获取外部 job_id 键?
【问题讨论】:
-
versions表中有哪些字段?另外请发布您的完整错误堆栈跟踪。 -
您使用的是哪个版本的 Rails?
-
我使用的是 Rails 4.2.1
-
你试过给
:job_id而不是self.job_id吗? -
成功了!作为符号传入时它是如何工作的?
标签: ruby-on-rails rails-activerecord rails-models