自动加载常量时检测到循环依赖(ActiveRecord)

时间:2014-11-17 14:45:22

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

我已经手动指定了模型中的关系(没有找到如何从ERD模型或现有数据库自动生成它们的方法),而是尝试使用移民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

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

1 个答案:

答案 0 :(得分:0)

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