# Simple ActiveRecord question

**URL:** <https://www.ruby-forum.com/t/simple-activerecord-question/121406>\
**Category:** Rails\
**Created:** [November 27, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406 "2007-11-27T18:29:07Z")\
**Posts on this page:** 6\
**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, 6:29pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406/1 "2007-11-27T18:29:07Z")

</div>

I’ve programmed in ruby for awhile, but I’m just getting into AR. I  
can boil my current problem understanding it down to the following  
example.

Thanks,  
Kyle

#!/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 how come  
pocket.things.find(:all)  
#throws some huge error (below) instead of finding the things in the  
pocket?

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:  
things.container\_id: SELECT \* FROM things WHERE (things.container\_id =  
2)  
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/connection\_adapters/abstract\_adapter.rb:128:in `log' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/connection_adapters/sqlite_adapter.rb:145:in`execute’  
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/connection\_adapters/sqlite\_adapter.rb:346:in  
`catch_schema_changes' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/connection_adapters/sqlite_adapter.rb:145:in`execute’  
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/connection\_adapters/sqlite\_adapter.rb:165:in  
`select_all' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:427:in`find\_by\_sql’  
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/base.rb:997:in `find_every' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:418:in`find’  
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/associations/has\_many\_association.rb:91:in `find’  
from (irb):68

---

<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:36pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406/2 "2007-11-27T18:36:44Z")

</div>

On Nov 27, 2007 11:28 AM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> create\_table :things do |table|  
> table.column :description, :string

You need a container\_id field here.

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

belongs\_to requires a foreign key.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<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:37pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406/3 "2007-11-27T18:37:55Z")

</div>

On 27 Nov 2007, at 17:28, Kyle S. wrote:

> require ‘active\_record’  
> end  
> end
> 
> class Container\<ActiveRecord::Base  
> has\_many :things  
> end
> 
> class Thing\<ActiveRecord::Base  
> belongs\_to :container  
> end

This is not enough: you do actually need to create the container\_id  
column on the things table.

Fred

---

<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:39pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406/4 "2007-11-27T18:39:05Z")

</div>

On Nov 27, 2007, at 11:28 AM, Kyle S. wrote:

> #but how come  
> pocket.things.find(:all)

The “find all” is automatic. You can just magically use the  
pocket.things array without having to prepare it.  
And if you have a Thing and you want to associate it with a Pocket,  
all you have to do is pocket.things \<\< thing and AR will update thing  
and save it.

---

<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:42pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406/5 "2007-11-27T18:42:00Z")

</div>

I was too hasty in my reply. Listen to what Greg said 🙂

---

<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:57pm UTC](https://www.ruby-forum.com/t/simple-activerecord-question/121406/6 "2007-11-27T18:57:20Z")

</div>

Fred, Greg, thanks so much. Just that simple addition made it behave  
the way I thought it should 🙂

create\_table :things do |table|  
table.column :description, :string  
table.column :container\_id, :int  
end

George, now that I’ve got that line, it does indeed work like I’d  
hoped.  
Sweet.

So, now the next question, how would I find a container by it’s  
contents?

Container.find\_by\_name(“Pocket”)  
#seems easy enough, but I can’t do…  
Container.find\_by\_thing  
#OK Container.column\_names only has id and name, so I can make an easy  
guess why _that_ didn’t work…  
#but how would you get something like finding Containers by things to  
work? Or is the solution really just to  
#search the Thing class for container\_id?

Thanks all  
–Kyle
