【问题标题】:Associations not working协会不工作
【发布时间】:2014-02-07 02:30:23
【问题描述】:

当用户在我的应用程序中取消他们的帐户时,他们的用户记录已从数据库中正确删除,但他们的个人资料记录似乎仍然存在。以下是我的用户和个人资料模型,有什么解决方案吗?

个人资料.rb

 class Profile < ActiveRecord::Base
    belongs_to :user
 end

user.rb

  class User < ActiveRecord::Base
   has_one :profile

   #callback that creates a profile for each user that signs up
   after_create :create_profile
   devise :database_authenticatable, :registerable,
   :recoverable, :rememberable, :trackable, :validatable

  private
    def create_profile
     self.profile = Profile.create
    end
  end

【问题讨论】:

    标签: ruby-on-rails activerecord devise associations


    【解决方案1】:

    您必须明确指定如何处理依赖模型。

    举例

    has_one :profile, dependent: :destroy
    

    还有一些其他选项,例如:delete:nullify。您可以在这里查看它们:http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_one

    【讨论】:

      【解决方案2】:

      它也会删除关联。

      has_one :profile, dependent: :destroy
      

      来源:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多