This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/99
yuguiさんのhas_one :throughはABD的にRailsを使うのには欠かせない存在です。 今回は、オリジナルのhas_one :throughを拡張して、Unsaved objectも扱えるようにしてみました。
class ActiveRecord::Associations::HasOneThroughAssociation <
ActiveRecord::Associations::HasOneAssociation
private
〜 略 〜
def load_target
if !loaded? && @owner.new_record?
table_name = @reflection.through_reflection.table_name
@target = @owner.send(table_name.singularize).send(@reflection.name)
end
super
end
end
上記のコードをオリジナルのコードに追加すると、Unsaved objectに対しても、アソシエーションを参照することができるようになります。
member = Member.new member.build_membership(:group => Group.new) member.groupというわけで、yuguiさんお願いしますー。
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/99
posted by
genki on Mon 11 Sep 2006 at 23:31 with 0 comments