Ruby on rails Rails:在Rails迁移中从模型调用方法
正在尝试从Ruby on rails Rails:在Rails迁移中从模型调用方法,ruby-on-rails,ruby,ruby-on-rails-3,migration,Ruby On Rails,Ruby,Ruby On Rails 3,Migration,正在尝试从scenario.rb调用一个方法,该方法包含一个名为complete\u scenario的方法?它在迁移文件中返回一个布尔值,但错误表明complete\u scenario?是一个未定义的\u方法 class AddCompleteFlagToScenarios < ActiveRecord::Migration def up change_table :scenarios do |s| s.boolean :complete, s.complete_scenario
scenario.rb
调用一个方法,该方法包含一个名为complete\u scenario的方法?它在迁移文件中返回一个布尔值,但错误表明complete\u scenario?
是一个未定义的\u方法
class AddCompleteFlagToScenarios < ActiveRecord::Migration
def up
change_table :scenarios do |s|
s.boolean :complete, s.complete_scenario? :default => false, :null =>false
end
end
def down
remove_column :scenarios, :complete
end
end
class AddCompleteFlagToScenariosfalse,:null=>false
结束
结束
降下
删除列:场景,:完成
结束
结束
是否有什么我做错了或忘记包括在内的事情?最后,我想向场景添加一个名为complete的新列,该列从场景中获取布尔值并将其放入更新的表中。
谢谢
class Scenario < ActiveRecord::Base
validates :name,
presence: true,
uniqueness: { :case_sensitive => false },
length: { in: 4..60 }
has_many :nodes
has_many :showings, -> { visible }
has_many :courses, :through => :showings
attr_accessor :warnings
amoeba do
enable
include_association [:nodes]
end
...
def complete_scenario?
(self.unlabeled_choices.empty?) && (self.no_goal_nodes?) && (self.regular_leaf_nodes.empty?) && (self.unconnected_nodes.empty?)
end
类场景false},
长度:{in:4..60}
有多个:节点
有很多:展示,->{visible}
有很多:课程,:至=>:展示
属性访问器:警告
阿米巴原虫
使可能
包含关联[:节点]
结束
...
def完成情况?
(self.unlabeled\u choices.empty?&&(self.no\u goal\u nodes?&&&(self.regular\u leaf\u nodes.empty?&&(self.unconnected\u nodes.empty?)&(self.unconnected\u nodes.empty?)
结束
更改您的“向上”,使其如下所示:
def up
change_table :scenarios do |s|
#here s is not ActiveRecord object, rather refers to table, s.complete_scenario? is invalid here, so instead do below
s.boolean :complete, :default => false, :null =>false
end
#here you set the complete field for all rows in the table
Scenario.find_each do |s|
s.complete = s.complete_scenario?
s.save!
end
end
或
更好的方法是在db/seed.rb中填充/种子新列
将迁移文件中的“up”更改为以下内容:
def up
change_table :scenarios do |s|
s.boolean :complete, :default => false, :null =>false
end
end
在db/seed.rb中添加这一行(或者您可以为以下内容使用单独的rake任务):
更改您的“up”,使其如下所示:
def up
change_table :scenarios do |s|
#here s is not ActiveRecord object, rather refers to table, s.complete_scenario? is invalid here, so instead do below
s.boolean :complete, :default => false, :null =>false
end
#here you set the complete field for all rows in the table
Scenario.find_each do |s|
s.complete = s.complete_scenario?
s.save!
end
end
或
更好的方法是在db/seed.rb中填充/种子新列
将迁移文件中的“up”更改为以下内容:
def up
change_table :scenarios do |s|
s.boolean :complete, :default => false, :null =>false
end
end
在db/seed.rb中添加这一行(或者您可以为以下内容使用单独的rake任务):
不建议在迁移中引用模型名称,因为模型容易发生更改和失效,而迁移应保持静态,并且在发生更改时可能会产生影响 所以,一般来说,
ActiveRecord::Migration
不应该关注ActiveRecord::Base
我不知道
未标记的\u选择
,无目标\u节点?
,常规的\u叶子\u节点
或未连接的\u节点
在内部做什么,但是,如果可能,您应该使用适当的(一系列)数据库连接若要填充:请使用完成属性。不建议在迁移中引用模型名称,因为模型容易更改和失效,而迁移预计将保持静态,并且在发生更改时可能会产生影响
所以,一般来说,ActiveRecord::Migration
不应该关注ActiveRecord::Base
我不知道未标记的\u选择
,无目标\u节点?
,常规的\u叶子\u节点
或未连接的\u节点
在内部做什么,但是,如果可能的话,你应该使用适当的(一系列)数据库连接来填充:complete
属性。实际上,我这样做解决了这个问题:
def up
add_column :scenarios, :complete, :boolean
Scenario.find_each do |s|
s.complete = s.complete_scenario?
s.save!
end
end
def down
remove_column :scenarios, :complete
end
实际上我是这样解决的:
def up
add_column :scenarios, :complete, :boolean
Scenario.find_each do |s|
s.complete = s.complete_scenario?
s.save!
end
end
def down
remove_column :scenarios, :complete
end
不建议在迁移中引用模型名称,因为模型易受更改和失效的影响,而迁移应保持静态,并且在发生更改时可能会产生影响。不建议在迁移中引用模型名称,因为模型易受更改和失效的影响,然而,迁移预计将保持静态,并且当迁移发生变化时,可能会产生影响。强烈建议在迁移中直接引用ActiveRecord::Base
模型的方法。请查看此响应:强烈建议在迁移中直接引用ActiveRecord::Base
模型。请查看此答复: