Last active
September 23, 2015 04:24
-
-
Save oNguyenNgocTrung/55831769e6440cb6ccf3 to your computer and use it in GitHub Desktop.
Question 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreatePeople < ActiveRecord::Migration | |
def change | |
create_table :people do |t| | |
t.string :name | |
t.references :parent, index: true | |
t.timestamps null: false | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 . Chỉ cần column People cho việc thực hiện migration tạo bảng cho Person. | |
2. update model Person: | |
class Person < ActiveRecord::Base | |
belongs_to :parent, class: Person | |
has_many :children, class: Person, foreign_key: :parent_id | |
has_many :grandchildren, class: Person, through: :children, source: :children | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person < ActiveRecord::Base | |
has_many :children, class_name: Person, foreign_key: :parent_id | |
belongs_to :parent, class_name: Person | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment