Railsチュートリアル第2版の第11章のリスト11.45では、クラスのfrom_users_followed_by
クラスメソッドはMicropost
次のように定義されています。
class Micropost < ActiveRecord::Base
...
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
end
この章の脚注13には、このブログ投稿へのリンクがあり、副選択文字列を作成する場合construct_finder_sql
は、メソッドでActiveRecord内部メソッドを使用できると述べています。そこで、文字列を次send
のように置き換えようとしました。followed_user_ids
followed_user_ids = Relationship.send(:construct_finder_sql,
select: "followed_id",
conditions: { follower_id: :user_id })
唯一の問題は、それconstruct_finder_sql
がRails 3で減価償却されたということです。そのため、私が書いたものがまったく正しいかどうかわからないだけでなく、とにかくそれを使用することはできません。:user_id
それで、この状況で機能するActiveRecord(できればパラメーターを使用できる)を使用して副選択文字列を作成するRails 3の方法はありますか?