Active record

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 * FROMbooksWHERE (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:inexecute’
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:inselect_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:infind_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:infind’
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:infind_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:inmethod_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

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.

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 :slight_smile:
Al paese mio si dice “da solo me la canto e da solo me la suono”
bye, joxer