ruby on rails - ArgumentError: Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key -
after trying rake:db migrate, got error in terminal
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 running task --trace)
when run rails s, error in terminal
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) /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' /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' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:82:in `validate_options' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:62:in `initialize' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/collection_association.rb:18:in `initialize' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:47:in `new' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:47:in `create_builder' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:35:in `build' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations.rb:1258:in `has_many' /app/models/user.rb:6:in `<class:user>' /app/models/user.rb:1:in `<top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:494:in `load_missing_constant' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:261:in `const_get' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:261:in `block in constantize' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:259:in `each' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:259:in `inject' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/inflector/methods.rb:259:in `constantize' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:566:in `get' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:597:in `constantize' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise.rb:302:in `get' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:80:in `to' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:75:in `modules' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:92:in `routes' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:159:in `default_used_route' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/mapping.rb:69:in `initialize' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise.rb:336:in `new' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise.rb:336:in `add_mapping' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:in `block in devise_for' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `each' /usr/local/lib/ruby/gems/2.2.0/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `devise_for' /config/routes.rb:24:in `block in <top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:423:in `instance_exec' /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:423:in `eval_block' /usr/local/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:401:in `draw' /config/routes.rb:1:in `<top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load' /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' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:40:in `each' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:40:in `load_paths' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:16:in `reload!' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:26:in `block in updater' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/file_update_checker.rb:75:in `call' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/file_update_checker.rb:75:in `execute' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:27:in `updater' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application/finisher.rb:69:in `block in <module:finisher>' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers' /usr/local/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each' /usr/local/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component' /usr/local/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from' /usr/local/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component' /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `each' /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `call' /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component' /usr/local/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each' /usr/local/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!' /config/environment.rb:5:in `<top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require' /config.ru:3:in `block in <main>' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:55:in `instance_eval' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:55:in `initialize' /config.ru:in `new' /config.ru:in `<main>' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:49:in `eval' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:49:in `new_from_string' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:40:in `parse_file' /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' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/server.rb:208:in `app' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/server.rb:61:in `app' /usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/server.rb:336:in `wrapped_app' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/server.rb:139:in `log_to_stdout' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/server.rb:78:in `start' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>' /bin/rails:8:in `require' /bin/rails:8:in `<top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in `load' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in `call' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client/command.rb:7:in `call' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/client.rb:26:in `run' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/bin/spring:48:in `<top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/binstub.rb:11:in `load' /usr/local/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/binstub.rb:11:in `<top (required)>' /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' /bin/spring:13:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>'
i have following active record models:
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 model
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 state :requested # im not seeing event set state state :accepted #if want trigger request event state add event :request, after: proc.new { send_request_email } transitions to: :requested end #if want trigger accepted state state event :accept, after: proc.new { send_acceptance_email } transitions to: :accepted end #if want trigger accepted state requeste state event :accept, after: proc.new { send_acceptance_email } transitions from: :requested, to: :accepted end end def self.request(user1, user2) transaction 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 migration file
class addstatetouserfriendships < activerecord::migration def change add_column :user_friendships, :state, :string, :whiny_transitions => false add_index :user_friendships, :state end end
createuserfriendships migration file
class createuserfriendships < activerecord::migration def change create_table :user_friendships |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
you should change code in user
model to
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
Comments
Post a Comment