# Belongs\_to. Association methods don't pass data to DB

**URL:** <https://www.ruby-forum.com/t/belongs-to-association-methods-dont-pass-data-to-db/190871>\
**Category:** Rails\
**Created:** [June 29, 2010, 5:31pm UTC](https://www.ruby-forum.com/t/belongs-to-association-methods-dont-pass-data-to-db/190871 "2010-06-29T17:31:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![polunochnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/65b543/32.png) [@polunochnik](https://www.ruby-forum.com/u/polunochnik)\
**Post date:** [June 29, 2010, 5:31pm UTC](https://www.ruby-forum.com/t/belongs-to-association-methods-dont-pass-data-to-db/190871/1 "2010-06-29T17:31:22Z")

</div>

Hi.

I have a problem with the association methods which passed to a model  
through a belongs\_to declaration. Here’s an illustration of the issue:

GIVEN:

# migration

class CreateArticlesAndAuthorsTables \< ActiveRecord::Migration  
def self.up  
create\_table :articles do |t|  
t.text :title  
t.integer :author\_id  
end

```
create_table :authors do |t|
  t.text :name
end

```

end

def self.down  
drop\_table :articles  
drop\_table :authors  
end  
end

# articles model

class Article \< ActiveRecord::Base  
belongs\_to :author  
end

# authors model

class Author \< ActiverRecord::Base  
has\_many :articles  
end

WHEN:

> > Article.create(:title =\> ‘one’).author = Author.create(:name =\> ‘Pavel’)

> > Article.create(:title =\> ‘two’).author = Author.find(:first, :conditions =\> {:name =\> ‘Pavel’})

THEN

sqlite\> select \* from authors;  
id = 1  
name = Pavel

sqlite\> select \* from articles;  
id = 1  
title = ‘one’  
author\_id = null

```
       id = 2
    title = 'two'

```

author\_id = null

Why do I have null values instead of ids as foreign keys in the  
articles table?

Thanks.

Ruby 1.9.2;  
Rails 2.3.8.

---

<div class="post-metadata">

**Author:** ![polunochnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/65b543/32.png) [@polunochnik](https://www.ruby-forum.com/u/polunochnik)\
**Post date:** [June 29, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/belongs-to-association-methods-dont-pass-data-to-db/190871/2 "2010-06-29T18:31:06Z")

</div>

I got it.

It should to break the creation statements onto a few steps:

WHEN:

> > one = Article.new(:title =\> ‘one’)

> > one.author = Author.new(:name =\> ‘Pavel’)

> > one.save

> > two = Article.new(:title =\> ‘two’)

> > two.author = Author.find(:first, :conditions =\> {:name =\> ‘Pavel’})

> > two.save

THEN:

sqlite\> select \* from authors;  
id = 1  
name = Pavel

sqlite\> select \* from articles;  
id = 1  
title = ‘one’  
author\_id = 1

```
       id = 2
    title = 'two'

```

author\_id = 1

---

<div class="post-metadata">

**Author:** ![polunochnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/65b543/32.png) [@polunochnik](https://www.ruby-forum.com/u/polunochnik)\
**Post date:** [June 29, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/belongs-to-association-methods-dont-pass-data-to-db/190871/3 "2010-06-29T20:20:35Z")

</div>

I was most interested about that methods which are added by belongs\_to  
declaration not has\_many.

Anyway, thanks for help, Colin.

---

<div class="post-metadata">

**Author:** ![polunochnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/65b543/32.png) [@polunochnik](https://www.ruby-forum.com/u/polunochnik)\
**Post date:** [June 29, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/belongs-to-association-methods-dont-pass-data-to-db/190871/4 "2010-06-29T18:49:15Z")

</div>

On 29 June 2010 17:29, P.A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > one.save
> 
> > > two = Article.new(:title =\> ‘two’)
> 
> > > two.author = Author.find(:first, :conditions =\> {:name =\> ‘Pavel’})
> 
> > > two.save

Have a look at  
author.articles.build  
which will build a new article for an author and save some messing  
about.

Colin
