- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在尝试 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
用户友谊模型
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
最佳答案
您应该将 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
关于ruby-on-rails - ArgumentError : Unknown key: :conditions. 有效键是 : :class_name, :class, :foreign_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488202/
sanitize 是什么意思在 Rails 中是什么意思? 我正在阅读 CanCanCan 的文档.它说: When using strong_parameters or Rails 4+, you
在过去的几个月里,我感觉自己对 Ruby on Rails (RoR) 开发的了解达到了极限。我为大/小客户和 friend /爱好项目开发了大大小小的应用程序。我知道如何开发这些应用程序,但开始感觉
我昨天参加了一个关于扩展 Rails 的聚会,其中一个主题是 Hexagonal Rails。然而,我只做了一年的 Rails,对 MVC 结构非常满意(也许太舒服了),所以我不太了解适配器和消息队列
我使用多个 Rails 应用程序,一些在 Rails 3.2/Ruby 2.0 上,一些在 Rails 2.3/Ruby 1.8.7 上。 他们的共同点是,随着他们的成长和添加更多的依赖项/ gem
这个问题在这里已经有了答案: Using Rails-UJS in JS modules (Rails 6 with webpacker) (5 个答案) 关闭 3 年前。 我正在尝试使用 UJS
我正在开发一个当前使用 Rails 1.2 的 Rails 应用程序,所以我现在离最新的稳定版本(Rails 2.3)还有很长的路要走。 我应该如何进行迁移到更新版本的 Rails 的过程? 我应该一
尝试按照 Ryan Bates Backbone.js 教程构建抽奖应用程序,但我已经遇到了第一段代码的问题。在 application.js 的 init 函数中,他初始化了 Raffler 路由的
我正在使用 Rails 3.2 并且我有一个数据库表,我想在其中找到符合以下条件的所有行: a = true and b = true and ( 0 true, :b =>
我有一个用户类和一个联系人,其中联系人是用户的子类。这两个类都存储在用户表中。 我的联系人可能有也可能没有电子邮件地址,而我的用户需要一个电子邮件地址(我的用户模型定义中有 validates_pre
我正在编写一个教程,我在其中演示了一些 rails 命令。在我的机器上 rails和 script/rails两者都同样有效。有“首选”形式吗?两者中哪一个更普遍? 最佳答案 当您运行 rails 时
我正在寻找有关通过我的应用程序前进的最佳方式的建议,这是我首次开始集成Elasticsearch。我是一名初学者,但是热衷于深入研究,以便原谅任何明显的错误! 我遵循了http://www.sitep
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我有 Rails 4,这是我的默认版本(我仍然希望它是)。但我不想在我的电脑上添加 rails 3.2。在以下命令中:gem install rails -v 3.2.16 我有这个警告: railt
您好,我想使用 Sheevaplug 构建一个“Rails Brick”来自 Marvell(操作系统是开箱即用的 Ubuntu,但您可以在其上安装其他发行版)。它将成为家庭服务器和静音、低成本(99
我需要能够从 Rails 控制台发送我的 Rails 应用程序的 Postgres 数据库中所有未接受的邀请。 (我有一个名为 Invitations 的表,其中包含一个名为 accepted 的 b
validate :cannot_modify_if_locked, on: :update def cannot_modify_if_locked if self.locked erro
我正在学习教程(学习 Rails 播客),需要更改以下路由语法,以便它与 Rails 3.0 兼容。谁能帮忙? map.view_page ':name', :controller => 'viewe
我是一名优秀的程序员,十分优秀!