2015年4月2日のブックマーク (3件)

  • 【Rails】has_many, throughの逆の関連はdelegate, toかhas_one, through

    TL;DR テーブル構成 Railsモデル構成 has_many, through の定義 belongs_to, through は使えない? 1. delegate を使う方法 2. has_one, through を使う方法 includes も使うことができる どちらの方法が良いか? 参考 TL;DR has_many+throughの逆の関連の定義には: belongs_to+throughは使えない delegate or has_one+through が使える has_one+through の方が効率もよく、 includes も使えてオススメ テーブル構成 とあるRailsアプリケーションでこんなテーブル構成があったとします。 Railsモデル構成 ユーザー(User)は複数の記事(Post)をもっていて、その記事は複数のコメント(Comment)を持っている、という

    【Rails】has_many, throughの逆の関連はdelegate, toかhas_one, through
    noir_neo
    noir_neo 2015/04/02
  • [悪徳商法?支店]: [rails] 複数のbelongs_toを一度に設定する

    モデルのassociationを使ってcreateすると、belongs_toも自動的に設定されます。しかし、3者間関係のように複数のbelongs_toがある場合、それらを自動的に設定するためには一工夫必要になります。 書籍(book)が、会社に所属する著者(author)と会社(company)に所有されている場合で考えて見ます。そのままcreateすると、bookのcompany_id はnil になります。 普通の解決法は、bookのcreate時にcompanyを明示的に指定する方法です。 ちょっと気の利いた解決法としては、associationを使ってbuildしたものを、concatする方法があります。 has_manyに条件を設定することで、railsがbelongs_toの関係を知ることが出来、一度に設定できるようになります。has_many :through の時にも使

    noir_neo
    noir_neo 2015/04/02
  • TechCrunch

    All business crowdfunding platforms wishing to operate within the European Union (EU) must now comply with a new EU-wide regulatory framework that brings a uniform set of rules to the bloc. Crowdfundi

    TechCrunch
    noir_neo
    noir_neo 2015/04/02