【问题标题】:Changing Model associations after running migration运行迁移后更改模型关联
【发布时间】:2013-05-09 08:26:00
【问题描述】:

我有一个包含以下内容的 Devise User 模型,我确实为其运行了迁移。

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable, :lockable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :role
  # attr_accessible :title, :body


  ROLES = ['admin', 'network/system admin', 'manager', 'programmer']

  def role?(base_role)
    ROLES.index(base_role.to_s) <= ROLES.index(role)
  end

end

稍后,我将以下两行添加到同一模型中,并为 Ticket、Projects 和 Assignments 运行迁移。

  has_many :projects, :through => :assignments
  has_many :tickets

以上是否更新了用户与门票和项目的关联?运行迁移后更改模型中的关联是否有任何问题?我想知道它,因为我现在正在开发 Rails 应用程序。

谢谢:)-

【问题讨论】:

  • 取决于您要对关联进行的更改。能否请您附上一些代码?
  • 我确信我已经提出了一个真实而明确的问题。投反对票的请说明原因。
  • @Dogbert:我已经更新了这个问题。请看一下

标签: ruby-on-rails model rails-migrations model-associations


【解决方案1】:

你也应该有关联...

has_many :assignments

在您的用户模型中。

无需其他更新。

【讨论】:

  • 关联会反映到数据库吗?
  • 不,因为关联是 has_many。因此,用户表中没有数据库更新。
猜你喜欢
  • 1970-01-01
  • 2021-07-04
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2021-07-06
  • 2016-12-10
  • 2015-10-12
相关资源
最近更新 更多