ArgumentError:未知密钥::条件。有效密钥为:: class_name,:class,:foreign_key

时间:2015-07-18 05:47:58

标签: ruby-on-rails ruby

尝试rake后:db migrate,我在终端

中收到此错误
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, :foreign_type
/app/models/user.rb:8:in `<class:User>'
/app/models/user.rb:1:in `<top (required)>'
/config/routes.rb:24:in `block in <top (required)>'
/config/routes.rb:1:in `<top (required)>'
/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

当我运行rails s时,这是我在终端中遇到的错误

Exiting
/usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/core_ext/hash/keys.rb:75:in `block in assert_valid_keys': 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, :foreign_type (ArgumentError)
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/core_ext/hash/keys.rb:73:in `each_key'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/core_ext/hash/keys.rb:73:in `assert_valid_keys'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:82:in `validate_options'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:62:in `initialize'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/collection_association.rb:18:in `initialize'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:47:in `new'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:47:in `create_builder'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:35:in `build'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations.rb:1258:in `has_many'
    from /app/models/user.rb:6:in `<class:User>'
    from /app/models/user.rb:1:in `<top (required)>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:261:in `const_get'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:261:in `block in constantize'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:259:in `each'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:259:in `inject'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:259:in `constantize'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:566:in `get'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:597:in `constantize'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise.rb:302:in `get'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:80:in `to'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:75:in `modules'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:92:in `routes'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:159:in `default_used_route'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:69:in `initialize'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise.rb:336:in `new'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise.rb:336:in `add_mapping'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:in `block in devise_for'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `each'
    from /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `devise_for'
    from /config/routes.rb:24:in `block in <top (required)>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:423:in `instance_exec'
    from /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:423:in `eval_block'
    from /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:401:in `draw'
    from /config/routes.rb:1:in `<top (required)>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:40:in `each'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/file_update_checker.rb:75:in `call'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/file_update_checker.rb:75:in `execute'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /usr/local/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
    from /config/environment.rb:5:in `<top (required)>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /config.ru:3:in `block in <main>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:55:in `instance_eval'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:55:in `initialize'
    from /config.ru:in `new'
    from /config.ru:in `<main>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:49:in `eval'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:49:in `new_from_string'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:40:in `parse_file'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/server.rb:208:in `app'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/server.rb:61:in `app'
    from /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/server.rb:336:in `wrapped_app'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/server.rb:78:in `start'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /bin/rails:8:in `require'
    from /bin/rails:8:in `<top (required)>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in `load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in `call'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client/command.rb:7:in `call'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client.rb:26:in `run'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/bin/spring:48:in `<top (required)>'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/binstub.rb:11:in `load'
    from /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/binstub.rb:11:in `<top (required)>'
    from /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /bin/spring:13:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

我有以下有效记录模型:

class User < ActiveRecord::Base
    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

end

UserFriendship模型

class UserFriendship < ActiveRecord::Base
    belongs_to :user
    belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'


    include AASM

    aasm column: 'state', initial: :pending, whiny_transitions: true do
      state :requested # im not seeing any event which set this state
      state :accepted

      #if you want an to trigger request event from any state simply add this
      event :request,   after: Proc.new { send_request_email }       do transitions to: :requested end


      #if you want to trigger accepted state from any state do this
      event :accept,   after: Proc.new { send_acceptance_email }       do transitions to: :accepted end

      #if you want to trigger accepted state from only requeste state
      event :accept,   after: Proc.new { send_acceptance_email }      do transitions from: :requested,   to: :accepted  end

    end

    def self.request(user1, user2)
      transaction do
        friendship1 = create!(user: user1, friend: user2, state: 'pending')
        friendship2 = create!(user: user1, friend: user2, state: 'requested')

        friendship1.send_request_email
        friendship1

      end
    end


    def send_request_email
        UserNotifier.friend_requested(id).deliver
    end

    def send_acceptance_email
        UserNotifier.friend_accepted(id).deliver 
    end



end

AddStateToUserFriendships迁移文件

class AddStateToUserFriendships < ActiveRecord::Migration
  def change
      add_column :user_friendships, :state, :string, :whiny_transitions => false do
      add_index :user_friendships, :state
  end
end

CreateUserFriendships迁移文件

class CreateUserFriendships < ActiveRecord::Migration
  def change
    create_table :user_friendships do |t|
        t.integer :user_id
        t.integer :friend_id
        t.timestamps null: false
    end

      add_index :user_friendships, [:user_id, :friend_id]
  end
end

UserFriendshipsController.rb

class UserFriendshipsController < ApplicationController
   before_action :authenticate_user!


    def new
        if params[:friend_id] 
            @friend = User.where(name: params[:friend_id]).first
            raise ActiveRecord::RecordNotFound if @friend.nil?
            @user_friendship = current_user.user_friendships.new(friend: @friend)
        else
            flash[:error] = "Friend required"
        end

    rescue ActiveRecord::RecordNotFound
        render file: 'public/404', status: :not_found
    end

    private

    def user_friendships_params
        params.require(:user_friendships).permit(:user, :friend, :user_id, :friend_id, :state)
    end

    def create
        if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
            @friend = User.where(name: params[:user_friendship][:friend_id]).first
            @user_friendship = current_user.user_friendships.new(friend: @friend) 
            @user_friendship.save
            redirect_to root_path
            flash[:notice] = "Friend Added"
        else
            flash[:error] = "Friend Required"
            redirect_to root_path
        end
    end

end

1 个答案:

答案 0 :(得分:10)

您应该将User模型中的代码更改为

class User < ActiveRecord::Base
  has_many :user_friendships
  has_many :friends, -> { where(user_friendships: { state: "accepted"}) }, through: :user_friendships
  has_many :pending_user_friendships, -> { where(state: "pending") }, class_name: 'UserFriendship', foreign_key: :user_id                                  
  has_many :pending_friends, through: :pending_user_friendships, source: :friend
end