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'

沒有留言: