den*_*iss 1 migration ruby-on-rails
每当我在Rails中更改模型时,是否需要重新迁移?有人能告诉我迁移真的有用吗?我正在阅读它,我对db/migrate中的内容与app/models中的内容之间的区别有些困惑.
例如,如果我在模型中添加has_one realtionship,是否需要重新迁移?为什么?
如果数据库发生更改,请使用迁移.如果您只是在模型中添加方法,则无需迁移.
例:
我们开始,我们只有first_name,last_name.我们希望将它们存储在数据库中,因此我们进行了以下迁移:
/app/models/human.rb
# empty
Run Code Online (Sandbox Code Playgroud)
/db/migrate/xxxxx.rb
add_column :humans, :first_name, :string
add_column :humans, :last_name, :string
Run Code Online (Sandbox Code Playgroud)
然后我们结婚了,所以我们想跟踪它
/app/models/human.rb
belongs_to :spouse
Run Code Online (Sandbox Code Playgroud)
/db/migrate/xxxxx.rb
add_column :humans, :spouse_id, :integer
Run Code Online (Sandbox Code Playgroud)
/app/models/offspring.rb
belongs_to :human
Run Code Online (Sandbox Code Playgroud)
/db/migrate/xxxxx.rb
create_table ...
Run Code Online (Sandbox Code Playgroud)
/app/models/human.rb
has_many :offspring
Run Code Online (Sandbox Code Playgroud)
/app/models/human.rb
def first_born
offspring.first
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
779 次 |
| 最近记录: |