【问题标题】:Circular dependency detected while autoloading constant (ActiveRecord)自动加载常量时检测到循环依赖 (ActiveRecord)
【发布时间】:2014-11-17 14:45:22
【问题描述】:

我已手动指定模型中的关系(尚未找到如何从 ERD 模型或现有数据库自动生成它们的方法),然后尝试使用 immigrant gem 生成包含 FK 的迁移。我得到:

rails generate immigration AddKeys
lib/active_support/dependencies.rb:478:in `load_missing_constant': Circular dependency detected while autoloading constant Assignment (RuntimeError)
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:180:in `const_missing'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:512:in `load_missing_constant'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:180:in `const_missing'

这是模型的代码:

class Account < ActiveRecord::Base
  include Person
  include Contact
  has_many :coworkers, :class_name => 'Coworker'
  has_many :customers, :class_name => 'Customer'
  has_many :locations, :class_name => 'Location'
  has_many :appointment_types, :class_name => 'AppointmentType'

  before_create :create_remember_token
  has_secure_password
  validates :password, length: { minimum: 6 }
  validates :rem_notice_hrs, presence: true
  validates :rem_notice_hrs, numericality: true
  validates :rem_text, presence: true
  validates :email, presence: true, length: { maximum: 255 },
        format: { with: VALID_EMAIL_REGEX }

  after_initialize :init

  def Account.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def Account.digest(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private
    def init
      if self.new_record?
        if self.rem_notice_hrs.nil?
          self.rem_notice_hrs = 24
        end
        if self.rem_text.nil?
          if self.company.nil?
            self.rem_text = "Dear [customer title: automatic] [customer family name: automatic], this is a reminder of your appointment with %{title} %{family_name} on [date/time]."
          else
            self.rem_text = "Dear [title] [customer family name], this is a reminder of your appointment with %{company} on [date/time]."
          end
        end
      end
    end

    def create_remember_token
      self.remember_token = User.digest(User.new_remember_token)
    end
end

class Location < ActiveRecord::Base
  belongs_to :coworker, :class_name => 'Coworker', :foreign_key => :wor_id
  belongs_to :appointment, :class_name => 'Appointment', :foreign_key => :app_id
  #optional deletion flag:
  validates_inclusion_of :deleted, in: [true, false]
end

class Coworker < ActiveRecord::Base
  include Person
  validates_inclusion_of :deleted, in: [true, false]
  belongs_to :account, :class_name => 'Account', :foreign_key => :acc_id
  has_many :assignments
end

class Location < ActiveRecord::Base
  belongs_to :coworker, :class_name => 'Coworker', :foreign_key => :wor_id
  belongs_to :appointment, :class_name => 'Appointment', :foreign_key => :app_id
  validates_inclusion_of :deleted, in: [true, false]
end

class Appointment < ActiveRecord::Base
  belongs_to :customer, :class_name => 'Customer', :foreign_key => :cus_id
  belongs_to :location, :class_name => 'Location', :foreign_key => :loc_id
  belongs_to :appointment_type, :class_name => 'AppointmentType', :foreign_key => :app_type_id
  has_many :assignments, :class_name => 'Assignment'
  has_many :reminders, :class_name => 'Reminder'

  validates_date :from, :on_or_after => :today
  validates_date :till, :on_or_after => :from
  validates_inclusion_of :deleted, in: [true, false]

  attr_accessor :title
end

上面的一些模型实现了以下关注点:

module Contact
  extend ActiveSupport::Concern
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations
  # phone attribute
  included do
    validates :phone, presence: true
    validates :phone, numericality: true
    attr_accessor :company
  end
  #email regex
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
end

module Person
  extend ActiveSupport::Concern
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations
  included do
   # deleted flag attribute
   attr_accessor :first_name, :family_name, :title
  end
end

你能说出这里出了什么问题吗?

【问题讨论】:

    标签: ruby-on-rails-4 rails-activerecord entity-relationship activemodel activesupport


    【解决方案1】:

    这是复制粘贴混乱的经典示例 - 我已将 Location.rb 的代码复制到 Assignment.rb 中,对其进行了编辑但忘记重命名类。所以我最终有两个 Location 模型 - 在 2 个不同的 rb 文件中。呵呵!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多