How chore can be associated to many models associations
I am a noob trying to make my first app. to allow my kids to log their chores. My question pertains to the tables I might envision:
chores (for the parents to post available chores and the value) kids (I have 3 kids and they will each have a need for 'credit' for work done) wallet (where the chores done by kids are stored)
my guess is that this works.
but the associations are confusing the heck out of me. A开发者_如何学编程ny help would be greatly appreciated.
Chore
has_many :kids :through => wallet
Kid
??
Wallet
has_many :kids
has_many :chores
wallet contains kid_id and chore_id
How about this:
First let model the fact that one chore can be associated to many kids
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
You can use Wallet for the name of the KidChore table if you like.
Second each Chore is done or not done
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
def self.down drop_table :chore end end
Now you want to now how much of the chore each kid has done
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
Hope this help.
精彩评论