# Rails - saving a 1 to many relationship in db

**URL:** <https://www.ruby-forum.com/t/rails-saving-a-1-to-many-relationship-in-db/69058>\
**Category:** Ruby\
**Created:** [February 16, 2006, 3:20am UTC](https://www.ruby-forum.com/t/rails-saving-a-1-to-many-relationship-in-db/69058 "2006-02-16T03:20:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 3:20am UTC](https://www.ruby-forum.com/t/rails-saving-a-1-to-many-relationship-in-db/69058/1 "2006-02-16T03:20:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 16, 2006, 6:51am UTC](https://www.ruby-forum.com/t/rails-saving-a-1-to-many-relationship-in-db/69058/2 "2006-02-16T06:51:31Z")

</div>

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
