Activerecord 由于属性更改而创建相关对象时发生NameError
以下是我试图实现的:我的项目有很多动作。当项目的状态更改时,创建新操作。稍后,我将询问一个项目的相关操作。不幸的是,当我试图通过状态更改进行操作时,遇到以下异常:Activerecord 由于属性更改而创建相关对象时发生NameError,activerecord,camping,Activerecord,Camping,以下是我试图实现的:我的项目有很多动作。当项目的状态更改时,创建新操作。稍后,我将询问一个项目的相关操作。不幸的是,当我试图通过状态更改进行操作时,遇到以下异常:namererror at/create;未初始化的常量::模型::项::操作 以下是我的模型: module Models class Item < Base has_many :actions def status=(str) @status = str actions.creat
namererror at/create;未初始化的常量::模型::项::操作
以下是我的模型:
module Models
class Item < Base
has_many :actions
def status=(str)
@status = str
actions.create do |a|
a.datetime = Time.now
a.action = str
end
end
end
class Actions < Base
belongs_to :item
end
class BasicFields < V 1.0
def self.up
create_table Item.table_name do |t|
t.string :barcode
t.string :model
t.string :status
end
create_table Actions.table_name do |t|
t.datetime :datetime
t.string :action
end
end
end
end
在提交更好的答案解释
Item::Action
的来源之前,我是如何修复它的:
module Models
class Item < Base
has_many :actions
def status=(str)
# Instance variables are not propagated to the database.
#@status = str
write_attribute :status, str
self.actions.create do |a|
a.datetime = Time.now
a.action = str
end
end
end
# Action should be singular.
#class Actions < Base
class Action < Base
belongs_to :item
end
class BasicFields < V 1.0
def self.up
create_table Item.table_name do |t|
t.string :barcode
t.string :model
t.string :status
end
create_table Action.table_name do |t|
# You have to explicitly declare the `*_id` column.
t.integer :item_id
t.datetime :datetime
t.string :action
end
end
end
end
模块模型
类项<基
有很多:行动
def状态=(str)
#实例变量不会传播到数据库。
#@状态=str
写入属性:状态,str
self.actions.create do|a|
a、 datetime=Time.now
a、 动作=str
终止
终止
终止
#行动应该是单一的。
#集体诉讼
很明显,我是一个无名小卒。当你有
有很多:动作时,ActiveRecord会试图找到集体动作。
module Models
class Item < Base
has_many :actions
def status=(str)
# Instance variables are not propagated to the database.
#@status = str
write_attribute :status, str
self.actions.create do |a|
a.datetime = Time.now
a.action = str
end
end
end
# Action should be singular.
#class Actions < Base
class Action < Base
belongs_to :item
end
class BasicFields < V 1.0
def self.up
create_table Item.table_name do |t|
t.string :barcode
t.string :model
t.string :status
end
create_table Action.table_name do |t|
# You have to explicitly declare the `*_id` column.
t.integer :item_id
t.datetime :datetime
t.string :action
end
end
end
end