Tag: postgresql

在Rails中的一个ActiveRecord事务中更新多个记录

如何使用Rails中的事务块一次更新/保存模型的多个实例? 我想更新数百条记录的值; 每条记录的值都不同。 这不是一个属性的批量更新情况。 Model.update_all(attr:value)在这里不合适。 MyModel.transaction do things_to_update.each do |thing| thing.score = rand(100) + rand(100) thing.save end end save似乎发布了它自己的事务,而不是将更新批处理到周围的事务中。 我希望所有更新都进入一次重大交易。 我怎么能做到这一点?

Ruby Guard问题 – ‘请安装sqlite3适配器’ – railstutorial.org

我正在关注Ruby on Rails教程并且在测试部分中有些困惑,特别是 – 3.6.2 – 使用Guard进行自动测试 根据教程的部署到Heroku的说明,我已切换到Postgresql并从我的gemfile中删除了sqlite3并进行了一个bundle install以进行更新。 但是,一旦我跑了 捆绑执行警卫 我收到的消息是: /Users/username/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.3/lib/bundler/rubygems_integration.rb:147:in block in replace_gem’: Please install the sqlite3 adapter: gem install activerecord-sqlite3-adapter`(sqlite3不是bundle的一部分。将它添加到Gemfile。)(LoadError) 我很困惑为什么我需要这个,因为sqlite3已从我的环境中删除? 我的Gemfile如下: source ‘https://rubygems.org’ gem ‘rails’, ‘3.2.3’ gem ‘pg’, ‘0.12.2’ group :development, :test do gem ‘rspec-rails’, ‘2.9.0’ gem ‘guard-rspec’, ‘0.5.5’ end # Gems used only for assets and not required # […]

ActionView :: Template :: Error(PG :: UndefinedFunction:ERROR:运算符不存在:整数~~未知

我从MySQL切换到PostgreSQL用于Heroku。 现在我的搜索不起作用。 无法弄清楚操作员有什么问题。 ActionView :: Template :: Error(PG :: UndefinedFunction:ERROR:运算符不存在:整数~~未知。 2014-11-11T19:59:58.082607+00:00 app[web.1]: Processing by AllListingsController#search_listings as JS 2014-11-11T19:59:58.105074+00:00 app[web.1]: 4: 2014-11-11T19:59:58.102205+00:00 app[web.1]: ^ 2014-11-11T19:59:58.105066+00:00 app[web.1]: HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 2014-11-11T19:59:58.105083+00:00 app[web.1]: app/controllers/all_listings_controller.rb:14:in `search_listings’ 2014-11-11T19:59:58.110318+00:00 heroku[router]: at=info method=GET path=”/search_listings?q=500&_=1415735994648″ host=subleasy.herokuapp.com request_id=24a21078-d3fd-4bce-9afb-bfc9d976c0a7 fwd=”50.247.32.153″ […]

将表列添加到Group by子句 – Ruby on Rails – Postgresql

我正在尝试使用Heroku,显然Postgresql比聚合函数的SQL要严格得多。 当我向Heroku推送时,我收到一个错误,说明如下。 在另一个问题上,我问我收到了一些指导说我应该将列添加到我的group by子句中,我不知道该怎么做。 请参阅下面的完整错误和PostsControll#index。 SELECT posts.*, count(*) as vote_total FROM “posts” INNER JOIN “votes” ON votes.post_id = posts.id GROUP BY votes.post_id ORDER BY created_at DESC LIMIT 5 OFFSET 0): PostsController def index @tag_counts = Tag.count(:group => :tag_name, :order => ‘count_all DESC’, :limit => 20) conditions, joins = {}, :votes @ugtag_counts = Ugtag.count(:group => :ugctag_name, […]

gem install pg error:无法理解Yosemite w / Ruby 2.1.5上的kern.osversion`14.0.0′

我使用RVM安装Ruby-2.1.5并再次运行bundle。 现在pg gem将无法安装,我收到此错误: gem install pg -v ‘0.17.1’ — –with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config Building native extensions with: ‘–with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config’ This could take a while… ERROR: Error installing pg: ERROR: Failed to build gem native extension. /Users/diego/.rvm/rubies/ruby-2.1.5/bin/ruby -r ./siteconf20141120-33258-108chh6.rb extconf.rb –with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config Using config values from /Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config checking for libpq-fe.h… yes checking for libpq/libpq-fs.h… yes checking for pg_config_manual.h… yes checking for […]

查询忽略时间戳日期的时间范围

我正在尝试查询我的rails数据库(Postgres)中的购买表,我想查询时间范围。 例如,我想知道在所有日期的下午2点到下午3点之间进行了多少次购买。 此表中有一个created_at列,但我没有看到如何在不搜索特定日期的情况下完成此操作。 我试过了: Purchases.where(“created_at BETWEEN ? and ?”, Time.now – 1.hour, Time.now) 但这最终只会在那些时候搜索今天的日期。

使用Ruby on Rails在PostgreSQL中使用interval

我希望从我的Rails应用程序中将持续时间(2天,5年……)作为PostgreSQL中的间隔保存。 duration_min和duration_max都是“2天”或“5年”之类的值,因此它们每个都是一个间隔: def change create_table :times do |t| t.interval :duration_min t.interval :duration_max t.timestamps end end 但是在将数据类型设置为“interval”并且rake返回时,数据库迁移失败: undefined method ‘interval’ for # 如何定义表以接受(并理解)像“2天”这样的输入?

Heroku Rails Rake任务同步生产和本地DB

我正在尝试创建一个rake任务,这样我就可以直接输入“rake db:sync”来更新我的本地数据库以匹配生产。 此解决方案利用Heroku团队提供的代码: 使用PG备份导入和导出Heroku Postgres数据库 当我使用curl –output /tmp/latest.dump# {url}时,我的latest.dump文件中出现以下错误: AuthorizationQueryParametersErrorQuery-string authentication version 4 requires the X-Amz-Algorithm, X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, and X-Amz-Expires parameters.421FEFF763870123vlVr/ihmQiDgYIpdFFkuCgEP8Smvr2ks0wRkf89fJ8NfHfsBb92EVv40Q0NZuQIC 这是我正在使用的代码。 #lib/tasks/db_sync.rake namespace :db do desc ‘Pull production db to development’ task :sync => [:backup, :dump, :restore] task :backup do Bundler.with_clean_env { puts ‘Backup started…’ system “heroku pg:backups capture –app YOUR_APP_NAME” puts […]

耙子流产了! Gem :: LoadError:为数据库适配器指定’postgresql’

我是Rails的新手。 一切都还可以,直到我更改我的Gemfile才能使用heroku。 我在我的Gemfile中有pg但是在运行bundle exec rake test时我仍然遇到这个错误 我也不能使用rails控制台。 rake aborted! Gem::LoadError: Specified ‘postgresql’ for database adapter, but the gem is not loaded. Add `gem ‘pg’` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). /home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec’ /home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec’ /home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection’ /home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in ‘ /home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval’ /home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in […]

rails字段是JSON对象的数组?

我有一个Damage表,其中包含以下字段: 描述 date_of_damage 我想添加另一个字段来存储Damagepoints ,这些JSON对象看起来像这样: { top: 50, left: 100 } 顶部和左侧是damagepoint上damagepoint的坐标。 用户使用Javascript添加/删除damagepoints点。 但是,我想在一个字段中存储这些damagepoints的数组,如下所示: @damage.damagepoints = [{left: 40, top: 99}, {left: 100, top: 35}, {left: 150, top: 95}] 我不想使用具有has_many关系的Damagepoint表来执行此操作,因为对此的所有更改都将使用Javascript完成,如果用户创建或删除了Damagepoints ,我只想从客户端传递更新damagepoints组,并用新数组替换数据库中的旧数据库。 如果我使用has_many关系,我必须删除所有的Damagepoints并在每次更新数组时创建每个新点(因为它太复杂了,删除/添加特定的损坏点没有任何好处,因为我不喜欢我需要追踪历史)。 存储@damage.damagepoints (上图)等数据的最简单方法是什么? 我需要做的只是将它(通过控制器)传递给html5数据属性,以便Javascript可以使用它将现有的damagepoints添加到图表中(基于它们的坐标),然后传递更新的数组(当来自html5数据属性)当用户单击“保存”按钮时,通过AJAX调用返回控制器。 我正在使用Rails 4.2和Postresql。 谢谢!