Skip to content

Instantly share code, notes, and snippets.

@oNguyenNgocTrung
Last active September 23, 2015 04:24
Show Gist options
  • Save oNguyenNgocTrung/55831769e6440cb6ccf3 to your computer and use it in GitHub Desktop.
Save oNguyenNgocTrung/55831769e6440cb6ccf3 to your computer and use it in GitHub Desktop.
Question 3
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
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
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