ActiveRecord
私は本Pro Active Record: Databases With Ruby And Railsから学んでいます。この本は少し古くなっています ( 2007年に発行されました) が、集計に慣れるまで、これまでのところ問題はありませんでした。これを実践するために私が使用しているコードは、次のもので構成されています。
- 単純なsqlite3データベース
- そのデータベースのテーブルに対応するクラス
- 最初のクラスの「集約モデル」である 2 番目のクラス (*何と呼べばよいかわかりません*)
コードを以下に示します。
SQL:
CREATE TABLE songs(
id INTEGER PRIMARYKEY AUTOINCREMENTED,
title VARCHAR
);
ルビー:
ActiveRecord::Base.establish_connection(:database => "songs.db", :adapter => "sqlite3")
class Song < ActiveRecord::Base
composed_of :songinfo, :class_name => "SongInfo", :mapping => [%w(title title)]
end
class SongInfo
attr_accessor :title
def initialize title
@title = title
end
end
song.songinfo = SongInfo.new("Purple Haze")
# I first tried it out on an old (school owned) computer, with an outdated version of ActiveRecord.
# When I called the previous line on the school computer, it worked fine, but on my home computer, which is not old, I saw: DEPRECATION WARNING: You're trying to create an attribute `title,'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc.
song.save
song.title
song.title = "Voodoo Child"
song.save
自宅のコンピューターで上記のコードを呼び出すと がsong.title
返されますnil
が、学校のコンピューターでは曲の実際のタイトルが返されます。また、学校のコンピューターでSong.find(1)
は、ID が 1 の曲を返しますが、自宅のコンピューターでは、以下に示す非常に長いエラー メッセージを返します。
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: songs.: SELECT "songs".* FROM "songs" WHERE "songs"."" = ? LIMIT 1
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `initialize'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `new'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `prepare'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/sqlite_adapter.rb:253:in `block in exec_query'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/sqlite_adapter.rb:467:in `select'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/explain.rb:25:in `logging_query_plan'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/querying.rb:37:in `find_by_sql'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation.rb:171:in `exec_queries'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation.rb:160:in `block in to_a'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/explain.rb:25:in `logging_query_plan'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation.rb:159:in `to_a'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation/finder_methods.rb:376:in `find_first'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation/finder_methods.rb:122:in `first'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation/finder_methods.rb:334:in `find_one'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation/finder_methods.rb:310:in `find_with_ids'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/relation/finder_methods.rb:107:in `find'
from /Users/Solomon/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.4/lib/active_record/querying.rb:5:in `find'
from (irb):19
from /Users/Solomon/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'1.9.3-p125 :020 >
バージョン番号:
- ルビー:1.9.3
- アクティブレコード: 3.2.4
- OS : OS X ライオン 10.7
これを行うための非推奨で有効な方法を詳しく説明して、助けていただければ幸いです。
アップデート:
投稿の前半は完全に修正されています。ただし、実行しようとするとSong.find(1)
、まだエラーメッセージが表示されます。