顯示具有 through 標籤的文章。 顯示所有文章
顯示具有 through 標籤的文章。 顯示所有文章

2008年10月28日 星期二

has_many,through,source,source_type,foregin_key,class_name


#我發表過forum的comments
has_many :forum_comments, :through => :forums, :source => :comments


#forum 裡有我的 comments的 forum, 找出 forum array
has_many :forums_has_comments,
:through => :comments,
:source => :record,
:source_type => 'Forum'

#我在forum 裡所有的comments, 找出 comments array
has_many :comments_in_forums,
:foreign_key => :user_id,
:class_name => 'Comment',
:conditions => {:record_type => 'Forum'}

2008年8月21日 星期四

has_one, has_many ,through

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000979


has_one
  • Account#build_beneficiary (similar to Beneficiary.new("account_id" => id))
  • Account#create_beneficiary (similar to b = Beneficiary.new("account_id" => id); b.save; b)
has_many
  • Firm#clients.build (similar to Client.new("firm_id" => id))
  • Firm#clients.create (similar to c = Client.new("firm_id" => id); c.save; c)


Some examples:

[user model]
has_many :record_comments, :through => :records, :source => :comments, :order => "created_at DESC", :limit => 6

has_many :demands, :foreign_key => 'demander_id', :class_name => 'Call'
has_many :accepts, :foreign_key => 'accepter_id', :class_name => 'Call'

[call model]
belongs_to :demander, :foreign_key => 'demander_id', :class_name => 'User'
belongs_to :accepter, :foreign_key => 'accepter_id', :class_name => 'User'