jeudi 29 juin 2017

Rails: Call a method from a model in Rails Migration

Trying to call a method from scenario.rb which contains a method called complete_scenario? which returns a boolean inside the migration file but an error states that complete_scenario? is an undefined_method.

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

Is there something that I'm doing wrong or forgetting to include? Ultimately, I want to add a new column to Scenario called complete that takes the boolean from Scenario and puts it into the updated table. Thanks.

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

Aucun commentaire:

Enregistrer un commentaire