Tag: postgresql

在PostgreSQL中执行此小时的操作查询

我在RoR堆栈中,我必须编写一些实际的SQL来完成所有“打开”记录的查询,这意味着当前时间在指定的操作时间内。 在hours_of_operations表中,两个integer列opens_on和closes_on存储工作日,两个time字段opens_at和closes_at存储一天中的相应时间。 我做了一个查询,将当前日期和时间与存储的值进行比较,但我想知道是否有一种方法可以转换为某种类型的日期类型并让PostgreSQL完成其余的工作? 查询的内容是: WHERE ( ( /* Opens in Future */ (opens_on > 5 OR (opens_on = 5 AND opens_at::time > ‘2014-03-01 00:27:25.851655’)) AND ( (closes_on 5) OR ((closes_on = opens_on) AND (closes_at::time ‘2014-03-01 00:27:25.851655’)) OR ((closes_on = 5) AND (closes_at::time > ‘2014-03-01 00:27:25.851655’ AND closes_at::time < opens_at::time))) OR /* Opens in Past */ […]

PG :: UndefinedTable:错误:关系“…”不存在

在迁移时,我收到以下错误消息: PG::UndefinedTable: ERROR: relation “actioncodes” does not exist : ALTER TABLE “organizations” ADD CONSTRAINT “fk_rails_4ecaa2493e” FOREIGN KEY (“actioncode_id”) REFERENCES “actioncodes” (“id”) 我有组织的以下迁移文件: class CreateOrganizations < ActiveRecord::Migration def change create_table :organizations do |t| t.string :name, null: false, limit: 40 t.references :actioncode, index: true, foreign_key: true t.boolean :activated t.datetime :activated_at t.timestamps null: false end end end 对于Actioncodes,我有迁移文件: […]

SQLite3“LIKE”或PostgreSQL“ILIKE”的通用Ruby解决方案?

我使用SQLite3进行开发,使用PostgreSQL进行部署。 但是,我面临以下问题: 我使用SQLite3简单搜索: def self.search(search) if search find(:all, :conditions => [“style LIKE ? OR construction LIKE ?”, “%#{search}%”, “%#{search}%”]) else find(:all) end end 但是,它不适用于PostgreSQL ,我需要替换ILIKE的LIKE才能解决问题: def self.search(search) if search find(:all, :conditions => [“style ILIKE ? OR construction ILIKE ?”, “%#{search}%”, “%#{search}%”]) else find(:all) end end 是否有“Ruby方式”在任何数据库中进行这些搜索? 编辑 – 基于您的答案我不相信我会找到一个通用的Ruby解决方案。 我遵循了Ruby on Rails教程:通过示例学习Rails – 作者:Michael Hartl […]

连接集必须包含所有值但可能包含更多值的SQL

我有三个表格, sports和offers_sports表offers_sports 。 class Offer < ActiveRecord::Base has_and_belongs_to_many :sports end class Sport < ActiveRecord::Base has_and_belongs_to_many :offers end 我想选择包含一系列运动名称的优惠。 他们必须包含所有的sports但可能有更多。 让我们说我有这三个优惠: light: – “Yoga” – “Bodyboarding” medium: – “Yoga” – “Bodyboarding” – “Surfing” all: – “Yoga” – “Bodyboarding” – “Surfing” – “Parasailing” – “Skydiving” 鉴于arrays[“Bodyboarding”, “Surfing”]我想要得到medium而不是light 。 我尝试过这个答案,但结果是零行: Offer.joins(:sports) .where(sports: { name: [“Bodyboarding”, “Surfing”] }) .group(“sports.name”) […]

Rails 4 / postgresql 9.4 – 查询并更新数组中的ruby对象

我有一个方法(find_by_sql) 在数组中输出ruby对象,如下所示: [#] 如何在raw postgresql中更新数据库中对象的’deal_available’属性? 我尝试了不同的方法来编写它,但我偶然发现它非常具体:它是一个数组,里面有一个ruby对象,我必须设法告诉postgresql 更改deal_available的值:true to deal_available:false。 在本地,我试过:logger.debug“这是更新的结果对象: #{@deal_question[0]} 但我得到: # 以下是@deal_question的创建方式 @deal_question = Deal.find_by_sql( ” SELECT \”deals\”.* FROM \”deals\” WHERE (opportunity_id = #{@deal.id} AND deal_available = true) ORDER BY \”deals\”.\”id\” ASC LIMIT 1″ ) 我甚至没有postgresql查询在这里建议,因为首先我需要了解如何在数组中查询这个ruby对象。

未定义的局部变量或方法`root_path’用于#

我正在努力研究其他人创建的RoR项目。 我正在尝试登录并为项目创建新的用户名和密码。 不幸的是,我收到以下错误: undefined local variable or method `root_path’ for # 这是我的database.yml文件: development: adapter: postgresql encoding: unicode database: name_of_app_development pool: 5

Ruby Datamapper .count始终返回0

每当我尝试从datamapper计算返回的记录时,它总是返回0,无论是否有用户。 User.count(:username=>params[:username]) class User include DataMapper::Resource property :id, Serial property :username, String, unique_index: true, required: true, length: 3..32 property :password, String, required: true, length: 5..64 property :email, String, unique_index: true, required: true, format: :email_address end

如何在rails中解析和显示hstore键/值

我有一个postgres hstore存储json字符串的数据类型。 我希望能够在rails html页面上解析并显示键/值。 不知何故,我只是不知道如何在数据字段上运行解析并显示字符串中列出的每个键/值。 <– this is the hstore json string <– this is an error, just to show 例如,payment_account.data包含{“hello”=>“world”,“great”=>“job”} 这是index.html.erb代码。 https://gist.github.com/axilaris/9174206 我该怎么做才能做到这一点? 解析hstore字符串以在rails中显示查询结果?

错误:表“用户”上的更新或删除违反表“评论”上的外键约束“fk_rails_03de2dc08c”

我坚持这个错误,无法弄清楚什么是错的。 铁轨已中止! ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation:错误:表“user”上的更新或删除违反了表“comments”上的外键约束“fk_rails_03de2dc08c”DETAIL:Key(id)=(11)仍然从表“comments”引用。 :DELETE FROM“users”WHERE“users”。“id”= $ 1#31

当我尝试做rake db:migrate时,我收到一个错误:ActiveRecord :: NODatabaseError角色“ubuntu”不存在

我通过这样做启动了postgresql服务器: sudo service postgresql start 然后我连接到服务: sudo sudo -u postgres psql 然后我创建了一个数据库(我正在尝试为我的应用添加一个投票系统): postgres=# CREATE DATABASE “votes”; 但我仍然有同样的问题。 还有,当我这样做的时候 rake db:create 我得到一个角色“ubuntu”不存在错误 这是我的database.yml: # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem ‘sqlite3’ # # default: &default # adapter: sqlite3 # pool: 5 […]