<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>Aerialarts &#187; ActiveRecord</title>
	<atom:link href="http://aerial.st/tags/activerecord/feed/" rel="self" type="application/rss+xml" />
	<link>http://aerial.st</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 01:27:51 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/tags/activerecord/feed/" />
		<item>
		<title>ActiveRecord3.1/mysql2でTRUNCATE</title>
		<link>http://aerial.st/archive/2011/12/07/activerecord31-mysql2-truncate/</link>
		<comments>http://aerial.st/archive/2011/12/07/activerecord31-mysql2-truncate/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 23:32:04 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[ActiveRecord]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1188</guid>
		<description><![CDATA[DatabaseCleanerを使うと 複数データベース（各種RDBMSやMongoDB、CouchDBなどなど）に対応した方法でデータを削除できるんだけど、複数のDBにコネクションをはっている場合にはうまく動いてくれないことがあるらしい。 でも自分の場合、mysql2でうまく動いてくれればそれでいいし、わざわざDatabaseCleanerを使う必要はないよなぁ…。ということで、ActiveRecord::ConnectionAdapters::Mysql2AdapterにTRUNCATEを発行してくれるメソッドを生やしてみた。実質DatabaseCleanerのコピペ。 module ActiveRecord module ConnectionAdapters class Mysql2Adapter &#60; AbstractAdapter def truncate_tables tables_to_truncate.each do &#124;table_name&#124; truncate_table(table_name) end end def truncate_table(table_name) execute("TRUNCATE TABLE #{quote_table_name(table_name)};") end private def tables_to_truncate tables - views - ["schema_migrations"] end def views @views &#124;&#124;= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue [] end end end end 次のようにtruncate_tablesを呼ぶと、ビューとschema_migrations以外に対してTRUNCATEを発行してくれる。 ActiveRecord::Base.connection.truncate_tables]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/12/07/activerecord31-mysql2-truncate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/12/07/activerecord31-mysql2-truncate/" />
	</item>
		<item>
		<title>ActiveRecord3.1/MySQLでBIGINT UNSIGNEDなid</title>
		<link>http://aerial.st/archive/2011/12/06/activerecird31-mysql2-unsigned-bigint-pk/</link>
		<comments>http://aerial.st/archive/2011/12/06/activerecird31-mysql2-unsigned-bigint-pk/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 02:44:36 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1184</guid>
		<description><![CDATA[ActiveRecord 3.1でMySQL2アダプタを使っている場合に、idカラム（プライマリキーのカラム）にBIGINT UNSIGNEDを使う方法。 まずCREATE TABLE時のidカラムの型を変更するために、ActiveRecordが読み込まれた直後、rakeタスク等でテーブルの作成が行われる前に次のように上書きする。 ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = \ "BIGINT UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY" あとは各マイグレーションファイルで、外部キー（*_idってやつ）のカラムの型にBIGINT UNSIGNEDを指定する。 t.column :user_id, "BIGINT UNSIGNED" これでidカラムも*_idカラムもBIGINT UNSIGNEDで作られる。 もし既存のテーブルの型を書き換えたい場合には、 change_column :users, :id, "BIGINT UNSIGNED" でいけると思う（未確認）。 ただ、BIGINT UNSIGNEDだと0から1846京まで使えるらしいので、DB単体では破綻するような気がする。 参考 How to use long id in Rails applications? &#8211; stackoverflow]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/12/06/activerecird31-mysql2-unsigned-bigint-pk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/12/06/activerecird31-mysql2-unsigned-bigint-pk/" />
	</item>
		<item>
		<title>ActiveRecordとMongoidは共存できる</title>
		<link>http://aerial.st/archive/2011/07/31/use-activerecord-and-mongoid/</link>
		<comments>http://aerial.st/archive/2011/07/31/use-activerecord-and-mongoid/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 16:36:32 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[mongoid]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1123</guid>
		<description><![CDATA[第5回 MongoDB.jp 勉強会 in Tokyoに行ってきた！ メモとかはまたのちほどアップするとして（そういえばRubyKaigi最終日のメモもアップできていない）、@yuki24 さんの発表で「ActiveRecordとMongoidは共存」できるという話を聞いて、ちょっと試してみた。 その過程で適当に作ったコードをgithubに上げておく。Todoを作成・更新した際にその内容をTodoLogに吐き出すというモノ。 mongoidのインストール これは普通に。 $ gem install bson_ext mongoid $ rails g mongoid:config Mongoidのモデルの作成 mongoidをインストールするとmodelジェネレータがmongoidのモデルファイルを生成してくれる。 $ rails g model todo_log title:string done:boolean invoke mongoid create app/models/todo_log.rb ActiveRecordのモデルの作成 modelジェネレータがmongoidに割り振られたので、代わりにactive_record:modelジェネレータを使う。 $ rails g active_record:model todo title:string done:boolean create db/migrate/20110730071022_create_todos.rb create app/models/todo.rb これはrails gコマンドの出力を見て初めて気づいた。 アプリでやってること Todoのafter_create、after_updateコールバックにTodoLog.createを追加すれば、TodoLが作成・更新された際にTodoLogが作成される。 最後に やってみれば当たり前なんだけどちゃんと動く。 mongoid入れただけでmodelがそれ用のジェネレータになっていたのでできないものだと思ってた。 調べてみるってとても大事。 あとスキーマレスは便利。ついさっき、「TodoLogなんだからtodo_id入れなきゃな」と気づいたのでコードをちょっとだけ修正したのだけれど、それだけでtodo_idが保存されるようになった。凄く手軽。 もちろん後から追加したんだから、それ以前のレコードにはtodo_idは保存されていないんだけど。]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/07/31/use-activerecord-and-mongoid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/07/31/use-activerecord-and-mongoid/" />
	</item>
		<item>
		<title>has_manyな関連先をまとめてINSERTする</title>
		<link>http://aerial.st/archive/2011/06/11/insert-has-many-relations/</link>
		<comments>http://aerial.st/archive/2011/06/11/insert-has-many-relations/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 03:00:51 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[actionview]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1081</guid>
		<description><![CDATA[fields_for、accepts_nested_attributes_forを使って、has_manyな関連先をまとめてINSERTする方法。ソースはgithubに上げておいた。ちなみにRails 3.0.8。 Post has_many Tags through Taggingsというモデルがあったとする。 とりあえずscaffoldはこんな感じ。Taggingだけは画面が要らないのでmodelだけ。 $ rails g scaffold posts title:string text:text $ rails g scaffold tags name:string $ rails g model tagging post:references tag:references $ rake db:migrate コードの修正で重要なのは次の2点。 app/models/post.rb に accepts_nested_attributes_forを設定する。 class Post < ActiveRecord::Base has_many :taggings has_many :tags, :through => :taggings accepts_nested_attributes_for :taggings end これで次のようなコードが実行された時にTaggingもまとめて作ってくれるようになる。 Post.create( :title => "タイトル", :text => "本文", :taggings_attributes => [ { :tag_id => 1 }, { :tag_id => 2 } ] ) app/views/posts/_form.html に fields_for を使ってリレーション先についてのフォームを作る。第一引数がtaggings_attributes[]になっているのがポイント。これはaccepts_nested_attributes_forに合わせて設定する。 &#60;div class="field"&#62; Tags&#60;br /&#62; [...]]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/06/11/insert-has-many-relations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/06/11/insert-has-many-relations/" />
	</item>
		<item>
		<title>named_scopeの自動生成＋お手軽検索</title>
		<link>http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/</link>
		<comments>http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 15:49:28 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=768</guid>
		<description><![CDATA[@ukstudioさんの「scopeでお手軽検索」 をRails2.3向けに少し修正1しつつ、カラムごとに一致検索をするようなnamed_scopeを自動生成するようにフック部分にしかけをした。 例えばnameカラムとageカラムを持ったUserモデルについて、 class User < ActiveRecord::Base extend Searchable end とすると、次のような感じでカラムの条件で簡単に検索が可能になる。 User.name("jsmith").first #=> User named jsmith User.search(:name => "jsmith", :age => 20..30) 追記 これだけだとwhereの導入された3.0では記法的なあまりメリットはないかもしれない。2.x系なら:conditionsをキーとしたハッシュが不要になるというメリットはあるけど。 やっぱり独自のnamed_scopeを作ってこそ便利になる気がする。 Rails2.3だとscopedに続けてnamed_scopeのメソッドを呼び出すとエラーが出るらしく、injectの引数にはselfのみを渡すようにした。&#160;&#8617;]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/" />
	</item>
		<item>
		<title>named_scopeの引数による条件分岐</title>
		<link>http://aerial.st/archive/2009/09/06/named_scope_can_varies_with_arguments_dynamically/</link>
		<comments>http://aerial.st/archive/2009/09/06/named_scope_can_varies_with_arguments_dynamically/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 17:00:31 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=545</guid>
		<description><![CDATA[named_scopeに引数を渡した場合、その型によって設定するSQLを動的に変更したいことがある。 こういうときにはlambdaに渡しているのブロック中で条件分岐すれば良い。 named_scope :member, lambda { &#124;m&#124; case m when Member { :conditions => { :member_id => m.id } } when Integer { :conditions => { :member_id => m } } else {} end } 要するに、lambdaに渡したブロックの処理の結果、findに渡すHashパラメータが返ればそれでok、という感じなんじゃないかと。]]></description>
		<wfw:commentRss>http://aerial.st/archive/2009/09/06/named_scope_can_varies_with_arguments_dynamically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2009/09/06/named_scope_can_varies_with_arguments_dynamically/" />
	</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/


Served from: aerial.st @ 2012-05-23 01:17:44 -->
