标签: rails-activerecord

ActiveRecord :: NoDatabaseError:具有ID的本地用户不存在

开发一个仅限API的Rails应用程序来使用API​​中的数据,过滤掉许多字段,然后广播我们想要公开的字段.

我的应用程序正常返回数据,但我意识到在源API级别上,我们更改了我们正在公开的数据字段之一的数据类型.为了适应,我提交了我的工作,检查了一个新分支,运行迁移以更改数据字段的数据类型,决定我对它的工作方式不满意,回滚迁移,提交和签出主分支.该应用正常返回数据.但是,现在当我使用取景器的方法,如在Rails的控制台测试我的模型的记录.first,.last等等,我得到下面的错误.他们以前工作得很好.

我环顾四周,并没有看到任何关于这个特定错误的线程(他们似乎都在寻找显示页面的个别记录) - 虽然这个,这个线程(通过扩展)看起来最接近.检查我正在使用的数据库上的用户显示错误中的用户ID("501")不存在.

我做了什么,我需要做什么才能再次调用finder方法?我是否需要在我的数据库中的错误中创建引用的用户?(为什么它不是我一直用来连接到我的数据库的默认用户?错误中的用户ID来自哪里?它甚至重要吗?)

ps - 如果它是相关的,我曾经rake db:rollback根据这个线程回滚我的迁移.

提前致谢.

Rails控制台

.2.1 :001 > KoboApi.first
ActiveRecord::NoDatabaseError: local user with ID 501 does not exist

    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-postgis-adapter-3.1.4/lib/active_record/connection_adapters/postgis_adapter.rb:51:in `initialize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-postgis-adapter-3.1.4/lib/active_record/connection_adapters/postgis/create_connection.rb:37:in `new'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-postgis-adapter-3.1.4/lib/active_record/connection_adapters/postgis/create_connection.rb:37:in `postgis_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
    from /Users/toby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails rails-activerecord

21
推荐指数
3
解决办法
1万
查看次数

在Rails中暂时禁用SQL缓存?

我目前正在创建一个带有一些cronjobs等的Rails应用程序,但是我有一些问题,因为Rails缓存了sql.

所以任何人都知道如何在Rails中禁用SQL Cache?不是全局的,而是针对此代码.真的不想为每个模型创建一个方法,所以无论如何只是暂时禁用它?

Terw

sql caching ruby-on-rails rails-activerecord

20
推荐指数
3
解决办法
6171
查看次数

在Rails 3/ActiveRecord中构建LIKE查询时逃避%%的正确方法

我想将url字段与url前缀(可能包含百分号)匹配,例如.where("url LIKE ?", "#{some_url}%").什么是最多的Rails方式?

sql ruby-on-rails ruby-on-rails-3 rails-activerecord

20
推荐指数
3
解决办法
1万
查看次数

多个Rails中的条件

我正在我的Rails应用程序中实现用户搜索功能.但是,我不希望管理员出现在搜索结果中.

我正在尝试这个:

User.where(:admin => [nil, false], ["name LIKE ?", "%#{params[:query]}%"])
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

syntax error, unexpected ')', expecting tASSOC
Run Code Online (Sandbox Code Playgroud)

那么如何正确列出where括号内的子句呢?

ruby activerecord ruby-on-rails rails-activerecord

20
推荐指数
1
解决办法
3万
查看次数

Rails嵌套连接条件的Activerecord

我正在尝试用条件编写嵌套连接查询.

我现在的查询是:

Event.joins(:store => :retailer).where(store: {retailer: {id: 2}})
Run Code Online (Sandbox Code Playgroud)

哪个输出以下SQL:

   SELECT "events".* FROM "events" INNER JOIN "stores" ON "stores"."id" = "events"."store_id" INNER JOIN "retailers" ON "retailers"."id" = "stores"."retailer_id" WHERE "store"."retailer_id" = '---
:id: 2
'
Run Code Online (Sandbox Code Playgroud)

还有以下错误:

SQLite3::SQLException: no such column: store.retailer_id: SELECT "events".* FROM "events" INNER JOIN "stores" ON "stores"."id" = "events"."store_id" INNER JOIN "retailers" ON "retailers"."id" = "stores"."retailer_id" WHERE "store"."retailer_id" = '---
:id: 2
'
Run Code Online (Sandbox Code Playgroud)

它告诉我没有列store.retailer_id,但是,我可以运行以下查询,它将正常工作:

Event.first.store.retailer_id
  Event Load (0.2ms)  SELECT  "events".* FROM "events"   ORDER BY "events"."id" ASC LIMIT 1 …
Run Code Online (Sandbox Code Playgroud)

sql ruby-on-rails rails-activerecord

20
推荐指数
2
解决办法
2万
查看次数

我该怎么做呢?Model.where("created_at> =#{Time.now - 5.days}")

这一直困扰我一段时间......

我怎样才能串插一个datetime在Rails的ActiveRecord的查询?

# Works, but supeh ugleh:
Model.where("created_at >= ?", Time.now - 5.days)

# How do I do this?
Model.where("created_at >= #{Time.now - 5.days}")
# As is, it produces the following error message:
# ActiveRecord::StatementInvalid: PG::Error: ERROR:  syntax error at or near ...
Run Code Online (Sandbox Code Playgroud)

我关心的原因是代码可读性:

# I like this better:
Model.where("created_at >= #{Time.now - 5.days} OR" + \
            "updated_at >= #{Time.now - 3.days}")

# than this:
Model.where("created_at >= ? OR updated_at >= ?", Time.now - 5.days, Time.now …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails rails-activerecord

18
推荐指数
2
解决办法
3万
查看次数

Ruby-on-Rails:从模型中选择不同的值

文档:http: //guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

清楚地说明:

query = Client.select(:name).distinct
# => Returns unique names
Run Code Online (Sandbox Code Playgroud)

但是,当我在我的控制器中尝试时,我收到以下错误:

undefined method `distinct' for #<ActiveRecord::Relation:0xb2f6f2cc>
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,我想要不同的名称,如['George','Brandon'],而不是客户的实际记录.有什么东西我错过了吗?

ruby-on-rails rails-activerecord

18
推荐指数
4
解决办法
4万
查看次数

Heroku + Sidekiq:ActiveRecord :: StatementInvalid:PG :: UnableToSend:SSL SYSCALL错误:检测到EOF

嗨,我们正在使用独角兽和Sidekiq在Heroku的Cedar堆栈上运行.我们间歇性地得到以下错误

BurnThis ActiveRecord::StatementInvalid: PG::UnableToSend: SSL SYSCALL error: EOF detected

ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() SSL SYSCALL error: Connection timed out
Run Code Online (Sandbox Code Playgroud)

有没有人知道这些错误的直接原因是什么?与我们的数据库连接太多了吗?我们已经通过以下方式设置了分叉:

unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 30
preload_app true

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::

Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  # other setup
  if …
Run Code Online (Sandbox Code Playgroud)

connection-pooling heroku rails-postgresql rails-activerecord heroku-postgres

18
推荐指数
1
解决办法
5789
查看次数

我的应用程序不断创建数据库连接,我如何追踪原因?

我有一个在Heroku上运行的Ruby on Rails应用程序.我一直在日志中收到这些消息:

2015-05-05T16:11:14Z app[postgres.27102]: [AQUA] connection received: host=xx.xxx.xx.26 port=60278
2015-05-05T16:11:14Z app[postgres.27102]: [AQUA] connection authorized: user=postgres database=somedb
2015-05-05T16:11:14Z app[postgres.27103]: [AQUA] connection received: host=xx.xxx.xx.26 port=60291
2015-05-05T16:11:14Z app[postgres.27103]: [AQUA] connection authorized: user=postgres database=postgres
2015-05-05T16:11:18Z app[postgres.27104]: [AQUA] connection received: host=xx.xxx.x.166 port=54180
2015-05-05T16:11:18Z app[postgres.27104]: [AQUA] connection authorized: user=postgres database=somedb
2015-05-05T16:11:23Z app[postgres.27105]: [AQUA] connection received: host=xx.xxx.x.166 port=55488
2015-05-05T16:11:23Z app[postgres.27105]: [AQUA] connection authorized: user=postgres database=somedb
2015-05-05T16:11:28Z app[postgres.27106]: [AQUA] connection received: host=xx.xxx.x.166 port=56774
2015-05-05T16:11:28Z app[postgres.27106]: [AQUA] connection authorized: user=postgres database=somedb
2015-05-05T16:11:28Z app[postgres.27107]: [AQUA] connection received: host=xx.xxx.x.166 port=56854 …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails heroku puma rails-activerecord

18
推荐指数
1
解决办法
441
查看次数

change_column_default的可逆迁移在Rails中没有任何默认值

Rails的指南,活动记录迁移说,你可以做

change_column_default :products, :approved, from: true, to: false
Run Code Online (Sandbox Code Playgroud)

change在Rails中有一个方法,类似于以下内容:

change_column_default :people, :height, from: nil, to: 0
Run Code Online (Sandbox Code Playgroud)

意图从没有任何默认值,到默认值为零.

但是,当我尝试回滚时,我得到了

ActiveRecord::IrreversibleMigration: ActiveRecord::IrreversibleMigration
Run Code Online (Sandbox Code Playgroud)

考虑到我给Rails中fromto,为什么没有接受它?

我正在使用Rails 4.2.0.

ruby-on-rails rails-migrations ruby-on-rails-4 rails-activerecord

18
推荐指数
2
解决办法
9660
查看次数