# One to Many Relationship Issues

**URL:** <https://www.ruby-forum.com/t/one-to-many-relationship-issues/56642>\
**Category:** Rails\
**Created:** [April 2, 2006, 3:17am UTC](https://www.ruby-forum.com/t/one-to-many-relationship-issues/56642 "2006-04-02T03:17:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 2, 2006, 3:17am UTC](https://www.ruby-forum.com/t/one-to-many-relationship-issues/56642/1 "2006-04-02T03:17:28Z")

</div>

Hi,

I am having issues with a one to many relationship(it never works :-)).  
Jokes aside, I am getting an error:

Mysql::Error: #23000Cannot add or update a child row: a foreign key  
constraint fails: INSERT INTO adverts (`image_url`, `price`, `title`,  
`website_url`, `description`, `user_id`) VALUES(‘bb’, 22.0, ‘aa’,  
‘[www.drill.com/cordless](http://www.drill.com/cordless)’, ‘bb’, 0)

The user\_id(last column) needs to be 1, and its 0

Session dump: —  
flash: !ruby/hash:ActionController::Flash::FlashHash {}  
:user\_id: 1

models:  
class Advert \< ActiveRecord::Base  
belongs\_to :user  
end

class User \< ActiveRecord::Base  
has\_many :advert  
end

adverts controller:  
def create  
@advert = Advert.new(params[:advert])  
if @advert.save  
flash[:notice] = ‘Advert was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

So its seems that the user\_id is not passed into the SQL statement. Do I  
need to change my create function? What needs to be added. I thought  
Rails takes care of this type of plumbing…

Please help.

Here is the SQL Tables  
create table users (  
id int not null auto\_increment,  
name varchar(100) not null,  
hashed\_password char(40) null,  
email varchar(255) not null,  
city varchar(255) not null,  
state varchar(255) not null,  
phone varchar(255) not null,  
website varchar(255) not null,  
primary key (id)  
);

create table adverts (  
id int not null auto\_increment,  
title varchar(100) not null,  
description text not null,  
image\_url varchar(200) not null,  
price decimal(10,2) not null,  
website\_url varchar(255) not null,  
user\_id int not null,  
constraint fk\_adverts\_users foreign key (user\_id) references  
users(id),  
primary key (id)  
);

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 2, 2006, 3:33am UTC](https://www.ruby-forum.com/t/one-to-many-relationship-issues/56642/2 "2006-04-02T03:33:21Z")

</div>

Try something like @user.adverts.create (params[:advert])

On 4/1/06, david [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The user\_id(last column) needs to be 1, and its 0  
> class User \< ActiveRecord::Base  
> render :action =\> ‘new’  
> create table users (
> 
> primary key (id)  
> );
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Jeremy H.

> **[Jeremy Huffman, the Website - jeremyhuffman.com](https://jeremyhuffman.com/)**
>
> Jeremy Huffman, the Website
>         
>       
>     
>     
> Getting in Touch
> 
> You can email me if you can guess the addres...
