Forum: Ruby rails - saving a 1 to many relationship in db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aeb5e3e7aa7d9f1f18456e97897ada7c?d=identicon&s=25 unknown (Guest)
on 2006-02-16 03:20
(Received via mailing list)
I am trying to save a 1 to many relationship using rails.  My parent
record
(Event)  is saved fine, but the child records (Schedules) do not get
inserted into the db.  Can someone please tell me what I'm doing wrong?

Thanks,
Doug


class AdminController < ApplicationController
  def save

  	@event = Event.new
	@event.title = "Surfing 101"
	@event.description = "Introductory class for beginners."
	@event.location = "Sunset Beach"
	@event.save

	@sch1 = Schedule.new
	@sch1.start_time = "8:00am"
	@sch1.max_enrollment = 6

	@sch2 = Schedule.new
	@sch2.start_time = "10:00am"
	@sch2.max_enrollment = 4

	@schedules = Array.new([@sch1, @sch2])

	@event.schedules << @schedules
	@event.update

  end
end

class Event < ActiveRecord::Base
	has_many 	:schedules
	validates_presence_of :title, :description, :location, :active
end
class Schedule < ActiveRecord::Base
	belongs_to	:events
	validates_presence_of  :sch_time, :max_enrollment
end

create table events (
	id				int not null auto_increment,
	title				varchar(30) not null,
	description			varchar(200) not null,
	location			varchar(30) not null,
	active				boolean default true,
	primary key (id)
);

create table schedules (
	id				int not null auto_increment,
	event_id			int not null,
	start_time			varchar(30) not null,
	max_enrollment			int not null,
	constraint fk_event_schedule foreign key (event_id) references
events(id),
	primary key (id)
);


E-MAIL CONFIDENTIALITY NOTICE:
The contents of this e-mail message and any attachments are intended
solely
for the addressee(s) and may contain confidential and/or legally
privileged
information.  If you are not the intended recipient of this message or
if
this message has been addressed to you in error, please immediately
alert
the sender by reply e-mail and then delete this message and any
attachments.
If you are not the intended recipient, you are notified that any use,
dissemination, distribution, copying, or storage of this message or any
attachments is strictly prohibited.
Be223e60c56535a0e465b84243aeb0d1?d=identicon&s=25 Timothy Goddard (Guest)
on 2006-02-16 06:51
(Received via mailing list)
A large proportion of that is unneccesary. Try:

   def save

        unless @event = Event.create(:title => "Surfing 101",
:description => "Introductory class for beginners.", :location =>
"Sunset Beach")
          ... do your error stuff
        end

        unless @sch1 = @event.create_in_schedules(:start_time =>
"8:00am", :max_enrollment => 6) # A Time object would be better for
start time.
          ... do your error stuff
        end

        unless @sch2 = @event.create_in_schedules(:start_time =>
"10:00am", :max_enrollment => 4)
          ... do your error stuff
        end
  end
This topic is locked and can not be replied to.