Question

I'm a rails n00b and and attempting to create a seed file for my rails prototype. Here are my models:

class Planning::Visit < ActiveRecord::Base
  has_many :planning_tasks, class_name: "Planning::Task"
end

class Planning::Task < ActiveRecord::Base
  belongs_to :planning_visits, class_name: "Planning::Visit"
end

I'm seeding my visits with 20 entries and that is working no problem. I'd like to seed the tasks with several entries that have a random existing visit assigned to each one. I have tried each of the following with no luck:

FAIL

task_list.each do |name, title, description, ship, hours|
  @task = Planning::Task.create( name: name, title: title, description: description, ship: ship, manhours: manhours ) # Create the base task
  @visit = Planning::Visit.find(:all, select: 'id', order: 'RANDOM()', limit: 1)  # Find a random visit
  @task.planning_visit_id = @visit.id  # Assign the random visit to the Task
  @task.save # Save the task
end

FAIL

task_list.each do |name, title, description, ship, hours|
  Planning::Task.create( name: name, title: title, description: description, ship: ship, manhours: manhours, planning_visit_id: Planning::Visit.find(:all, select: 'id', order: 'RANDOM()', limit: 1) )
end

The tasks create but have no planning_visit_id assigned to them.

My schema is defined as:

create_table "planning_visits", force: true do |t|
  t.integer  "visit_number"
  t.date     "start_date"
  t.date     "end_date"
  t.string   "station"
  t.string   "ship"
  t.string   "priority"
  t.text     "notes"
  t.boolean  "lock_date"
  t.boolean  "lock_station"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.boolean  "has_issue"
  t.string   "issue_severity"
end

create_table "planning_tasks", force: true do |t|
  t.string   "name"
  t.string   "title"
  t.string   "description"
  t.string   "ship"
  t.integer  "manhours"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "planning_visit_id"
end

Would really appreciate some help here as I am running out of hair to rip out and walls to put holes in.

Was it helpful?

Solution

belongsTo is singular :planning_visit

check the documentation for belongsTo relation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top