83's

Top > Tags > ActiveRecord

ActiveRecord

タイトル一覧を表示 | 本文を表示

t.referencesで外部キーの定義をちょっと簡単に、しかし明確に書ける February 21, 2008 23:00

なんでかこういうちょっと気の利いたやつは知るとうれしくなってしまう。

マイグレーションでt.integer :tag_idと書くところをt.references :tagと書けるんだってさ。 確かに関連に使うフィールドはintのxxx_idって規約で決まってんだから、いちいちそんなこと書きたくないよね。 「referencesってintegerより長くなって指が絡まりそうだYO!」と言いたくなる衝動は :polymorphic => trueあたりを見てなだめていただきたい。DRY優先。 詳しくは以下。

TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type column if the :polymorphic option is supplied. If :polymorphic is a hash of options, these will be used when creating the _type column. So what can be written like this:

  create_table :taggings do |t|
    t.integer :tag_id, :tagger_id, :taggable_id
    t.string  :tagger_type
    t.string  :taggable_type, :default => 'Photo'
  end

Can also be written as follows using references:

  create_table :taggings do |t|
    t.references :tag
    t.references :tagger, :polymorphic => true
    t.references :taggable, :polymorphic => { :default => 'Photo' }
  end

ActiveRecord 2.0.xからかな。1の時からあったらショック。

SQL logic error or missing database October 27, 2007 23:45

むう。ActiveRecordでSQLite3使ってみてるんだけど、スレッドの数2つ以上にして読み書きしてると SQL logic error or missing databaseてのが出るよ。 書き込みが集中してるときに出てるみたい。スレッドセーフじゃないのかな……。

あとtimeout指定してるのにたまにSQLite3::BusyException飛んでくるし。 まったく! SQLiteはまったく!

一応バージョン書いておくか……。最新の3.5じゃないんだねubuntuタン。

% sqlite3 --version
3.4.2

そういえばRailsのconfig/database.ymlでtimeout書けるのをソース見て知ったよ。

development:
    adapter: sqlite3
    dbfile: db/development.db
    timeout: 10000

でもググると常識ぽい。

普通に使うときはこう。

  ActiveRecord::Base.establish_connection(
    :adapter  => "sqlite3",
    :dbfile => "foo.db",
    :timeout => 10000
  )

ActiveRecordを単体で利用する時にテストでfixtureを使うには March 11, 2007 23:42

ActiveRecordを単体で使うときってのは、 モデル自体はrequire "active_record"でいいわけだけど テストでfixture使おうと思ったらどうなるんだろうと思ってちょっと調べたのでメモ。 絶対忘れるからね!

ActiveRecord::Base.establish_connectionは今まで

  ActiveRecord::Base.establish_connection(
    :adapter  => "mysql",
    :host     => "localhost",
    :username => "myuser",
    :password => "mypass",
    :database => "somedatabase"
  )

みたいにオレはやってたんだけど、これじゃfixture使うにはダメみたい。

こうする。

ActiveRecord::Base.configurations = YAML.load_file(File.dirname(__FILE__) + "/database.yml")
ActiveRecord::Base.establish_connection

ここのdatabase.ymlはRailsのと同じ。まあdevelopment/test/productionでなくてもいいけど。 どれを使うかはRAILS_ENVで切替える (RAILS_ENVをdatabase.ymlから読んだハッシュのキーにするわけですな)。 だからRAILS_ENVestablish_connectionする前にセットしておかないといけない。 忘れるとActiveRecord::AdapterNotSpecified例外が投げられる。

これで準備が整ったので、テストでfixturesを使いたいなら いつものに加えてrequire "active_record/fixtures"でおk。

  1 $:.unshift File.dirname(__FILE__) + "/../prog"
  2 require "rubygems"
  3 require "test/unit"
  4 require "active_record"
  5 require "active_record/fixtures"
  6 
  7 RAILS_ENV = "test"
  8 require "connection" 
  9 
 10 require "morph"
 11 
 12 class MorphTest < Test::Unit::TestCase
 13   self.fixture_path = File.dirname(__FILE__)
 14   self.use_instantiated_fixtures = true
 15 
 16   fixtures :morphs
 17 
 18   def test_load_fixtures
 19     assert @morph1
 20     assert_equal "aa", @morph1.pos1
 21   end
 22 end

まだこれしか試してないけど、fixture読めてるんできっといいんだろう。

ちなみにrequire "connection"で上に書いた ActiveRecord::Base.establish_connectionをしてる。

今後モデルが増えるようなら、1〜8行目と13, 14は別ファイルに追い出しちゃえばいいね。

あー結局なんも作ってないから今日も研究進んでないけどまあいいや。

そう言えばRailsのdatabase.ymlはERB経由するんだね。豆知識フエタ\(^o^)/