# Failry simple(?) active record question

**URL:** <https://www.ruby-forum.com/t/failry-simple-active-record-question/121350>\
**Category:** Ruby\
**Created:** [November 27, 2007, 4:32am UTC](https://www.ruby-forum.com/t/failry-simple-active-record-question/121350 "2007-11-27T04:32:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [November 27, 2007, 4:32am UTC](https://www.ruby-forum.com/t/failry-simple-active-record-question/121350/1 "2007-11-27T04:32:52Z")

</div>

I’m plodding my way through active record right now, and running into  
some things that seem odd.

Take a really simple example  
#!/usr/bin/ruby  
require ‘rubygems’  
require ‘active\_record’

ActiveRecord::Base.establish\_connection(:adapter =\> “sqlite3”,  
:database =\> “foo.db”)

ActiveRecord::Schema.define() do  
create\_table :containers do |table|  
table.column :name, :string  
end  
create\_table :things do |table|  
table.column :description, :string  
end  
end

class Container\<ActiveRecord::Base  
has\_many :things  
end

class Thing\<ActiveRecord::Base  
belongs\_to :container  
end

Container.create(:name=\>“Bucket”)  
bucket=Container.find\_by\_name(“Bucket”)  
pocket=Container.create(:name=\>“Pocket”)

bucket.save  
pocket.save

Container.find(:all)  
#this will find both containers

Thing.find(:all)  
#empty, as expected

bucket.things.create(:description=\>“fish”)  
pocket.things.create(:description=\>“lint”)

Thing.find(:all)  
#looks good…

#but howcome  
pocket.things.find(:all)  
#throws some huge error instead of finding the things in the pocket?

Sorry for the overly newbie type question 🙂 I just really thought  
ActiveRecord was supposed to take care of all this stuff.

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [November 27, 2007, 6:20am UTC](https://www.ruby-forum.com/t/failry-simple-active-record-question/121350/2 "2007-11-27T06:20:58Z")

</div>

On Nov 26, 9:32 pm, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :things  
> bucket.save
> 
> Thanks,  
> Kyle

What error do you get? That would help in diagnosing the problem.

You might also want to ask the Ruby on Rails list. I know ActiveRecord  
doesn’t have to be used with Rails, but they probably have more people  
there with ActiveRecord experience.

Jeremy

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [November 27, 2007, 9:14am UTC](https://www.ruby-forum.com/t/failry-simple-active-record-question/121350/3 "2007-11-27T09:14:02Z")

</div>

On Nov 26, 2007, at 19:32 , Kyle S. wrote:

> I’m plodding my way through active record right now, and running into  
> some things that seem odd.

AR questions should go to the rails list… that’s the group that  
writes and supports AR.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [November 27, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/failry-simple-active-record-question/121350/4 "2007-11-27T15:14:17Z")

</div>

OK. I’ll repost it there. As I really wanna just use AR and not  
rails 😉 I had hopes for this list.

Heck maybe this just means if I’m having this much trouble I should  
write all my findings up as a few good examples and howto’s.

Thanks  
–Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [November 28, 2007, 6:03am UTC](https://www.ruby-forum.com/t/failry-simple-active-record-question/121350/5 "2007-11-28T06:03:24Z")

</div>

Kyle S. wrote:

> Take a really simple example
> 
> ActiveRecord::Schema.define() do  
> create\_table :containers do |table|  
> table.column :name, :string  
> end  
> create\_table :things do |table|  
> table.column :description, :string  
> end  
> end  
> …  
> #but howcome  
> pocket.things.find(:all)  
> #throws some huge error instead of finding the things in the pocket?

Kyle,

when you have a relationship ‘belongs\_to’, you need to define a field  
in the database table to references the parent (the name is  
\<parent\_singular\>\_id), as ActiveRecord does not create this field  
automatically. So, we just need a ‘container\_id’ in the ‘things’ table:

ActiveRecord::Schema.define() do  
create\_table :containers do |table|  
table.column :name, :string  
end

create\_table :things do |table|  
table.column :description, :string  
table.column :container\_id, :integer # \<-- here  
end  
end

Your example runs now fine.  
You could also add an index (with ‘add\_index’) for performance.

Regards

Raul
