【问题标题】:Model association error Rails Admin模型关联错误 Rails Admin
【发布时间】:2012-05-31 01:46:15
【问题描述】:

尝试安装 rails admin 时出现以下错误...假设它是模型关联错误,但我无法弄清楚..

 /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:119:in `compute_type': uninitialized constant Interest::Person (NameError)
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in `klass'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/adapters/active_record.rb:218:in `association_model_lookup'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/adapters/active_record.rb:63:in `block (2 levels) in associations'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/config/fields/association.rb:59:in `call'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/config/fields/association.rb:59:in `associated_model_config'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/config/fields/association.rb:34:in `block in <class:Association>'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/config/configurable.rb:68:in `instance_eval'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/config/configurable.rb:68:in `block in register_instance_option'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/rails_admin/config/configurable.rb:42:in `block in register_instance_option'
    from (erb):86:in `block (2 levels) in template'
    from (erb):85:in `map'
    from (erb):85:in `block in template'
    from (erb):82:in `map'
    from (erb):82:in `template'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/erb.rb:753:in `eval'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/erb.rb:753:in `result'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/file_manipulation.rb:111:in `block in template'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/create_file.rb:54:in `call'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/create_file.rb:54:in `render'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/create_file.rb:47:in `identical?'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/create_file.rb:73:in `on_conflict_behavior'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/empty_directory.rb:111:in `invoke_with_conflict_check'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/create_file.rb:61:in `invoke!'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions.rb:95:in `action'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/create_file.rb:26:in `create_file'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/actions/file_manipulation.rb:110:in `template'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/rails_admin-0.0.3/lib/generators/rails_admin/install_generator.rb:62:in `install'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `block in invoke_all'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `each'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `map'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/group.rb:226:in `dispatch'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/base.rb:389:in `start'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/generators.rb:170:in `invoke'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/generate.rb:12:in `<top (required)>'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:29:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

型号

class User < ActiveRecord::Base
  has_many :friends
  has_many :person_interests, :as => :person
  has_many :interests, :through => :person_interests
  has_many :user_holidays
  has_many :holidays, :through => :user_holidays
  has_many :anniversaries
end

class PersonInterest < ActiveRecord::Base
  attr_accessible :person_id, :person_type, :interest_ids
  belongs_to :interest
  belongs_to :person, :polymorphic => true

end

class Interest < ActiveRecord::Base
  attr_accessible :name
  has_many :person_interests
  has_many :people, :through => :person_interests

end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 associations rails-admin


    【解决方案1】:

    问题是因为您的has_many :people 关联在您的Interest 模型中是这样的:

    has_many :people, :through => :people_interest
    

    PersonInterest 模型中的person 关联肯定足够polymorphic,但has_many :through 不尊重这一点。

    因此,我建议尝试另一种方法来吸引所有人。你可以这样做:

    def people
      person_interests.includes(:person).map(&:people).flatten.uniq
    end
    

    【讨论】:

    • person_interests.includes(:person).map(&amp;:people).flatten.uniq
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多