0

我一直在学习Pro Active Record: Databases With Ruby And RailsActiveRecord一书。这本书有点过时(2007 年出版),但到目前为止我没有遇到任何问题,直到我学会了聚合。我用来练习的代码由以下部分组成:

  • 简单的 sqlite3数据库
  • 与该数据库中的表对应的类
  • 第二类是第一类的“聚合模型”(*不知道该怎么称呼它*)

代码如下所示:

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 X Lion 10.7

任何帮助将不胜感激,详细说明不推荐使用的有效方法。

更新:

帖子的前半部分完全固定。但是,当我尝试运行Song.find(1)时,它仍然会给出错误消息。

4

1 回答 1

1

我看到一个问题,映射中的一个额外逗号,这可能与您的问题无关。尝试这个:composed_of :songinfo, :class_name => "SongInfo", :mapping => [%w(title title)]

于 2012-06-14T22:53:14.803 回答