Edge Rails

[Edge Rails] join時のcondtionsをわかりやすく : Easy Join Table Conditions

2008/08/03

追記: すみません、join時だけでなく通常のfindでも使えるようです。

関連するモデルからデータを引くときに、↓のように:joinsオプションを使うことはよくあります。

1
2
3
4
5
6
7
8
9
10
11
class Article < ActiveRecord::Base
  belongs_to :user
end
 
class User < ActiveRecord::Base
  has_many :articles
end
 
# Get all the users that have published articles
User.find(:all, :joins => :article,
  :conditions => ["articles.published = ?", true])

今回のコミットでは、join時での:conditionsオプションをハッシュで指定できるようになりました。

1
2
3
# Get all the users that have published articles
User.find(:all, :joins => :article,
  :conditions => { :articles => { :published => true } })

綺麗に書けますが、これ使うかなあ。。


related posts


tags

, , ,


have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


« [Edge Rails] 入力文字数の長さを定義する : Custom Length Validation Tokenizer
» Neo Classics 2 が出る模様