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 Png genki on Mon 11 Sep 2006 at 23:31

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/98

ブログの記事をエントリーと呼ぶかアーティクルと呼ぶかというお話。

Railsで書けばこんな感じになります。

class Blog < ActiveRecord::Base
  has_many :entries
  has_many :articles, :through => :entries
end
エントリーは「記入する」というアクティビティで、ブログと記事(Article)はリソースなので、ABD的に考えると何の呵責もなくすっきりと理解できますね。すばらしい!

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/98

posted by Png genki on Mon 11 Sep 2006 at 15:13
Contents
has_one :throughでUnsaved Objectを扱う
エントリーとABD
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
瀧内元気: おお、チェックしてみます。thx! '11-12
overisland: Reeder for iPhone もこの UI を実装していますね。 '11-12
瀧内元気: その情報は見たのですが、以下のサイトによると、現在はまた必要になってるっぽいんですよね。 ... '11-12
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ