# Activerecord failed?

**URL:** <https://www.ruby-forum.com/t/activerecord-failed/90295>\
**Category:** Rails\
**Created:** [March 27, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/activerecord-failed/90295 "2007-03-27T15:00:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Yang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@Kevin\_Yang](https://www.ruby-forum.com/u/Kevin_Yang)\
**Post date:** [March 27, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/activerecord-failed/90295/1 "2007-03-27T15:00:17Z")

</div>

Hello all,  
i found something wrong (or maybe i was wrong?)  
this looks like a rectiverecord bug , can anyone help me to point  
out what’s going on?

------------------migration  
create\_table :books do |t|  
t.column :name,:string  
t.column :person\_id,:integer  
end

```
create_table :people do |t|
  t.column :name ,:string
end

```

------------------model  
class Book \< ActiveRecord::Base  
belongs\_to :person  
end

class Person \< ActiveRecord::Base  
has\_many :books  
end

------------------in console , try the following:

book=Book.create(:name=\>‘book1’)  
kevin=Person.create(:name=\>‘kevin’)  
kevin.books # should be empty []

book.person=kevin  
book.save

kevin.books # why still nothing ???

Person.find(kevin.id).books # have books!

---

<div class="post-metadata">

**Author:** ![Kevin\_Yang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@Kevin\_Yang](https://www.ruby-forum.com/u/Kevin_Yang)\
**Post date:** [March 27, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/activerecord-failed/90295/2 "2007-03-27T15:43:29Z")

</div>

because you already loaded the association when you did kevin.books  
the first time.

book = Book.create(…)  
kevin - Person.create(…)  
kevin.books # empty, now association is loaded, it won’t be reloaded  
unless you force it

book.person = kevin  
book.save

kevin.books(true) # passing true forces reload, should show associated  
books

---

<div class="post-metadata">

**Author:** ![Kevin\_Yang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@Kevin\_Yang](https://www.ruby-forum.com/u/Kevin_Yang)\
**Post date:** [March 27, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/activerecord-failed/90295/3 "2007-03-27T15:45:46Z")

</div>

> book=Book.create(:name=\>‘book1’)  
> kevin=Person.create(:name=\>‘kevin’)  
> kevin.books # should be empty []
> 
> book.person=kevin  
> book.save
> 
> kevin.books # why still nothing ???
> 
> Person.find(kevin.id).books # have books!

kevin.reload
