I'm trying to the has_one and has_many methods in one model. A taskflow can have many tasks but it also has a single default task.
I'm trying to create a column in the taskflow table that contains the id of a task. However when I try and set that default task it does not work.
class Taskflow < ActiveRecord::Base
has_many :tasks
has_one :default_task, :class_name => 'Task'
class Task < ActiveRecord::Base
belongs_to :taskflow
I seed the database then in the rails console I try to assign a task to be a taskflow default_task:
taskflow1 = Taskflow.first
task1 = Task.first
taskflow1.default_task = task1
This doesn't work with the taskflow default_task value remaining 'nil'. What the correct way to achieve the desired behaviour?
Any help would be much appreciated.
Edit
Migration files are:
class CreateTaskflows < ActiveRecord::Migration
def change
create_table :taskflows do |t|
t.string :title
t.string :description
t.references :default_task
t.timestamps null: false
end
end
end
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :task_type
t.text :help
t.text :data
t.belongs_to :taskflow
t.timestamps null: false
end
end
end