I have been learning ActiveRecord
from the book Pro Active Record: Databases With Ruby And Rails. The book is a little bit outdated (published in 2007), but I have had no problems with it so far until I had gotten up to aggregations. The code I have been using to practice this is made up of the following:
- Simple sqlite3 database
- Class corresponding to the table in that database
- Second class which is the "aggregation model" (*Not sure what to call it*) of the first class
The code is displayed below:
SQL:
CREATE TABLE songs(
id INTEGER PRIMARYKEY AUTOINCREMENTED,
title VARCHAR
);
Ruby:
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
When I call the preceding code on my home computer, song.title
returns nil
, while on the school computer, it returned the actual title of the song. Also, on the school computer, Song.find(1)
would return the song with an ID of 1, on the home computer, it returns a very long error message, displayed below:
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 >
Version Numbers:
- Ruby: 1.9.3
- ActiveRecord: 3.2.4
- OS: OS X Lion 10.7
Any help would be appreciated, detailing the non-deprecated, valid way of doing this.
UPDATE:
The first half of the post is completely fixed. When I try to run Song.find(1)
, though, it still gives the error message.