# Active record

**URL:** <https://www.ruby-forum.com/t/active-record/147629>\
**Category:** IT\
**Created:** [September 3, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/active-record/147629 "2008-09-03T19:46:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@joxer](https://www.ruby-forum.com/u/joxer)\
**Post date:** [September 3, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/active-record/147629/1 "2008-09-03T19:46:57Z")

</div>

Sto giochicchiando con active record e provavo le associazioni.  
Dovrei provare a trovare dentro la table books la colonna “username”  
dalla tabella Item da cui dipende.

L’errore è questo:

joxer@polly:~/ruby/activerecord$ ruby mysql\_association.rb  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/connection\_adapters/abstract\_adapter.rb:147:in  
`log': Mysql::Error: Unknown column 'books.item_id' in 'where clause': SELECT * FROM`books`WHERE (`books`.item_id = 1) (ActiveRecord::StatementInvalid) from /var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:299:in`execute’  
from  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/connection\_adapters/mysql\_adapter.rb:515:in  
`select' from /var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in`select\_all\_without\_query\_cache’  
from  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/connection\_adapters/abstract/query\_cache.rb:61:in  
`select_all' from /var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:582:in`find\_by\_sql’  
from  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/base.rb:1341:in  
`find_every' from /var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:536:in`find’  
from  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/associations/association\_collection.rb:47:in  
`find' from /var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_collection.rb:305:in`find\_target’  
from  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/associations/association\_collection.rb:259:in  
`load_target' from /var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_proxy.rb:173:in`method\_missing’  
from  
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active\_record/associations/association\_collection.rb:274:in  
`method\_missing’  
from mysql\_association.rb:21

il codice riguardante :

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

class Book \< ActiveRecord::Base  
belongs\_to :item  
end

a = Item.find( :first )  
a.books.each do |s|  
s.username  
end

Scusate la niubbagine ma non ho neanche trovato buoni tutorial in rete e  
ho visto perlopiù dalle doc.  
Grazie, joxer

---

<div class="post-metadata">

**Author:** ![joxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@joxer](https://www.ruby-forum.com/u/joxer)\
**Post date:** [September 4, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/active-record/147629/2 "2008-09-04T12:48:15Z")

</div>

Per aiutarci a comprendere la causa dell’errore ci potresti dire che  
colonne ci sono dentro le due tabelle?

Per dirne una, pare che in books manchi item\_id, senza la quale le due  
tabelle non possono essere associate.

---

<div class="post-metadata">

**Author:** ![joxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@joxer](https://www.ruby-forum.com/u/joxer)\
**Post date:** [September 6, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/active-record/147629/3 "2008-09-06T17:59:58Z")

</div>

Paolo M. wrote:

> Per aiutarci a comprendere la causa dell’errore ci potresti dire che  
> colonne ci sono dentro le due tabelle?
> 
> Per dirne una, pare che in books manchi item\_id, senza la quale le due  
> tabelle non possono essere associate.

Ho per fortuna risolto, grazie cmq dell’aiuto 🙂  
Al paese mio si dice “da solo me la canto e da solo me la suono”  
bye, joxer
