0

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.

Bobby Tables
  • 1,154
  • 4
  • 15
  • 25
  • What is the reason to create Song table and then create SongInfo class which is not connected to database? Do you like to make your life harder? – megas Jun 14 '12 at 22:23

1 Answers1

1

I see a problem, an extra comma in mapping, that may have nothing to do with your issue. Try this: composed_of :songinfo, :class_name => "SongInfo", :mapping => [%w(title title)]

Anil
  • 3,899
  • 1
  • 20
  • 28
  • Thanks, that fixed 1/2 of the problem actually :-) – Bobby Tables Jun 14 '12 at 23:27
  • @BobbyTables So what does it do now? – Anil Jun 14 '12 at 23:38
  • @BobbyTables Restart your server, create a new record, and then try it again please. – Anil Jun 14 '12 at 23:43
  • @BobbyTables Can you change the name of the title field? I have had strange behaviors with fields named `title` and `name` and `label` etc. In the meanwhile, I am poring over your code again. – Anil Jun 15 '12 at 02:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12584/discussion-between-anil-and-bobby-tables) – Anil Jun 15 '12 at 02:54