2011-04-26 50 views
0

我是一个小孩,试图让我的第一个应用...让我的孩子们登录他们的家务。我的问题涉及到的表,我可能想象:儿童杂事应用的导轨协会

琐事(为父母后可用做家务和值) 孩子(我有3个孩子,他们将各自拥有需要“信用”为完成工作) 钱包(孩子们完成的琐事存储在这里)

我的猜测是这个工程?

但这些关联令我感到困惑。任何帮助将不胜感激。

Chore 
has_many :kids :through => wallet 

Kid 
?? 

Wallet 
has_many :kids 
has_many :chores 

wallet contains kid_id and chore_id 

回答

0

如何:

先让模型的事实是一个苦差事可以关联到很多孩子

Kid: 
    has_many :kid_chore 
    has_many :chores, :through=>:kid_chore 

KidChore: 
    belongs_to :kid 
    belongs_to :chore 

Chore: 
    has_many :kid_chore 
    has_many :kid, :through=>:kid_chore 

,如果你愿意的话,可以使用电子钱包的KidChore表的名称。

其次每个家务已完成或没有完成

class Chore < ActiveRecord::Migration 
    def self.up 
    create_table :chore do |t| 
     t.string :name 
     t.date  :start_date 
     t.date  :end_date 
     t.string :status # done or not done 

     #...all the field that you like 

     t.timestamps 
    end 

    end 

高清self.down drop_table:苦差事 结束 结束

现在,你想现在多少琐事每个孩子做了

class KidChore < ActiveRecord::Migration 
    def self.up 
    create_table :kid_chore do |t| 
     t.integer :kid_id 
     t.integer :chore_id 
     t.integer :percentage_done # done or not done 
     #...all the field that you like 
     t.timestamps 
    end 

    end 

    def self.down 
    drop_table :kid_chore 
    end 

end

希望得到这个帮助。

+0

真的很有帮助...非常感谢。 – chris 2011-04-27 02:45:56