2010年11月18日 星期四

create one to many relationship

1.use has_one create connection with page on subject
#vim app/models/subject.rb

add new line in it
has_many :pages

2.use belongs_to create connection with subject on page
# vim app/models/page.rb

add two line in it
belongs_to :subject
has_many: sections

3.use belongs_to to create connection with page on section
add new line in it
belongs_to :page

3.use console create connection on subject with page
#rails console
subject = Subject.find(2)
page = Page.first
subject.pages << page

4.use size to get the account for subject
subject.pages.size
subject.pages.empty?
subject.pages.delete(page)
subject.pages[1]

沒有留言:

張貼留言