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
booksWHERE (
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