新方法(ActiveRecord Aggregations)

时间:2012-06-14 22:09:23

标签: ruby sqlite activerecord ruby-1.9 aggregation

我从Pro Active Record: Databases With Ruby And Rails一书中学习ActiveRecord。这本书有点过时(发表于 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 >

版本号:

      
  • Ruby :1.9.3
  •   
  • ActiveRecord :3.2.4
  •   
  • OS :OS X Lion 10.7

任何帮助都将受到赞赏,详细说明了不弃用的这种有效的方式。

UPDATE:

该帖子的前半部分已完全修复。但是,当我尝试运行Song.find(1)时,它仍会显示错误消息。

1 个答案:

答案 0 :(得分:1)

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