【问题标题】:rails 4.1 bundle exec rake test fail and now showing jumbles of errorsrails 4.1 bundle exec rake 测试失败,现在显示混乱的错误
【发布时间】:2014-12-29 10:07:01
【问题描述】:

您好,我对 Rails 有点陌生,我在模型中做了一些代码,在我尝试了代码之后

bundle exec rake test

测试文件然后控制台就这样炸毁了

rake aborted!
ArgumentError: Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/core_ext/hash/keys.rb:71:in `block in assert_valid_keys'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/core_ext/hash/keys.rb:69:in `each_key'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/core_ext/hash/keys.rb:69:in `assert_valid_keys'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations/builder/association.rb:81:in `validate_options'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations/builder/association.rb:61:in `initialize'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations/builder/collection_association.rb:18:in `initialize'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations/builder/association.rb:46:in `new'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations/builder/association.rb:46:in `create_builder'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations/builder/association.rb:35:in `build'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/associations.rb:1215:in `has_many'
/home/simplybel/projects/gamification/app/models/user.rb:5:in `<class:User>'
/home/simplybel/projects/gamification/app/models/user.rb:1:in `<top (required)>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:348:in `require_or_load'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:480:in `load_missing_constant'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:180:in `const_missing'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/inflector/methods.rb:238:in `const_get'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/inflector/methods.rb:238:in `block in constantize'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/inflector/methods.rb:236:in `each'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/inflector/methods.rb:236:in `inject'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/inflector/methods.rb:236:in `constantize'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:552:in `get'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:583:in `constantize'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise.rb:302:in `get'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/mapping.rb:80:in `to'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/mapping.rb:75:in `modules'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/mapping.rb:92:in `routes'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/mapping.rb:159:in `default_used_route'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/mapping.rb:69:in `initialize'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise.rb:336:in `new'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise.rb:336:in `add_mapping'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:in `block in devise_for'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `each'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `devise_for'
/home/simplybel/projects/gamification/config/routes.rb:12:in `block in <top (required)>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/routing/route_set.rb:337:in `instance_exec'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/routing/route_set.rb:337:in `eval_block'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/routing/route_set.rb:315:in `draw'
/home/simplybel/projects/gamification/config/routes.rb:1:in `<top (required)>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `block in load'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:40:in `each'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:16:in `reload!'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/file_update_checker.rb:75:in `call'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/file_update_checker.rb:75:in `execute'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:27:in `updater'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application/finisher.rb:71:in `block in <module:Finisher>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/initializable.rb:30:in `instance_exec'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/initializable.rb:30:in `run'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/initializable.rb:54:in `run_initializers'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/application.rb:300:in `initialize!'
/home/simplybel/projects/gamification/config/environment.rb:5:in `<top (required)>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/projects/gamification/test/test_helper.rb:2:in `<top (required)>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/projects/gamification/test/helpers/home_helper_test.rb:1:in `<top (required)>'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/simplybel/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run => test:units

这是我的模型中的代码

class User < ActiveRecord::Base
has_many :statuses
has_many :user_friendships
has_many :friends, through: :user_friendships,
                    conditions: { user_friendships: { state: 'accepted' }}
has_many :pending_user_friendships, class_name: 'UserFriendship',
                                    foreign_key: :user_id,
                                    conditions: { state: 'pending' }
has_many :pending_friends, through: :pending_user_friendships, source: :friend
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true
validates :last_name, presence: true
validates :profile_name, presence: true,
                     uniqueness: true,
                     format: { 
                        with: /\A[a-zA-Z0-9_-]+\z/,
                        message: 'Must be formatted correctly.'
                     }
def full_name
"#{first_name} #{last_name}"
end
def to_param
profile_name
end
def gravatar_url
stripped_email = email.strip
downcase_email = stripped_email.downcase
hash = Digest::MD5.hexdigest(downcase_email)
"http://gravatar.com/avatar/#{hash}"
end
end

我刚刚修改了模型并根据条件进行了调整,在我添加 :conditions 后它似乎失败了,任何帮助都可以做到这一点,非常感谢,也谢谢!

【问题讨论】:

    标签: ruby-on-rails model ruby-on-rails-4.1


    【解决方案1】:

    以这种方式在关联上指定条件已被弃用(我相信 4.0),后来被删除。

    目前的做法是

    has_many :friends, through: :user_friendships,
                    scope: -> { where(user_friendships: { state: 'accepted' })}
    

    以前的其他选项也是如此,例如orderselect

    【讨论】:

    • 您好,感谢您的回答,但请您详细说明一下,因为我刚开始编程,我只是按照教程进行操作,但问题是我无法正确理解如何使用这些条件
    • @user1868185 您的教程已过期。我根据您的具体情况更新了答案
    • 您好,再次感谢您的帮助,是的,我想是的,因为版本是 3.6 版,所以真的落后了。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多