rails:226
From: ABE Masahiro <abe@c...>
Date: Tue, 28 Jun 2005 13:06:51 +0900
Subject: [rails:226] has_manyで追加されたfind()の:condition
あべです。
has_manyで自動的に追加されたfindメソッドには、検索条件に
プレースホルダーを使った書き方が使えないようなのです。
(ActiveRecordのバージョンは1.10.1です)
#
# 男女パートナー換えをしつつダンスをした記録
#
class Person < ActiveRecord::Base
has_many :records
end
class Man < Person; end
class Woman < Person; end
class Record < ActiveRecord::Base
has_a :man
has_a :woman
end
# オレとあのコの記録を検索
@my = Man.find(1)
@her = Woman.find(5)
recs = Record.find(:all, :conditions => [ "man_id = ? AND woman_id = ?", @my.id, @her.id ])
# => ok
recs = @my.records.find(:all, :conditions => [ "woman_id = ?", @her.id ])
# => ng
You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax
to use near '?5' at line 1:
SELECT * FROM records WHERE records.man_id = 1 AND woman_id = ?5
「?5」のように配列の要素が文字列として単に連結されてしまっています。
ちなみに文字列の条件だとうまくいきます。
recs = @my.records.find(:all, :conditions => "woman_id = #{@her.id}")
# => ok
http://ar.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000009
には
collection.find - finds an associated object according to the
same rules as Base.find.
「same rules」とあるので、使えるのかなと思ったのですが...
--
ABE Masahiro <abe@c...>
--
ML: rails@r...
使い方: http://QuickML.com/
-> 226 2005-06-28 06:06 [abe@c... ] has_manyで追加されたfind()の:condition 227 2005-06-28 08:13 ┗[moriq@m... ]