Tag: postgresql

Rails控制台 – 查找在某一天创建的位置

使用Ruby on Rails控制台,是否可以在数据库中查询某一天创建的所有记录? 就像是 date = “january 5 2013” users = User.find(:all, :conditions => {:created_at => date})

Rails 4迁移:has_and_belongs_to_many表名

我目前尝试将rails 3.2应用程序切换到rails 4.0。 但是我对has_and_belongs_many模型有一个问题。 我创建了一个测试应用程序,我遇到了同样的问题。 这就是我所做的: 创建了两个模型:foo_clip和foo_url class FooClip < ActiveRecord::Base has_and_belongs_to_many :foo_urls attr_accessible :id, :name end class FooUrl < ActiveRecord::Base has_and_belongs_to_many :foo_clips attr_accessible :url end 在此之后,我更新了迁移文件: class CreateFooClips < ActiveRecord::Migration def change create_table :foo_clips do |t| t.string :name t.timestamps end end end class CreateFooUrls < ActiveRecord::Migration def change create_table :foo_urls do |t| t.string :url […]

使用postgresql gem async

我正在使用Goliath (由eventmachine驱动)和postgres gem pg ,目前我正以阻塞的方式使用pg gem: conn.exec(‘SELECT * FROM products’) (例如)和我想知道是否有更好的方式连接到postgres数据库?

rails 3.2.2(或3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10连接错误

我正在使用PostgreSQL 9.1.3(在x86_64-pc-linux-gnu上的PostgreSQL 9.1.3,由gcc-4.6.real(Ubuntu / Linaro 4.6.1-9ubuntu3)4.6.1,64位编译)和rails 3.2 ubuntu 11.10上的.2或3.2.1。 现在,我可以使用PostgreSQL连接下面的命令 su postgres 输入密码,我可以看到postgres =# 我在config / database.yml中放置了以下细节并执行“ rails db ”它工作正常。 development: adapter: postgresql encoding: utf8 reconnect: false database: sample_app_db pool: 5 username: postgres password: passwordhere host: localhost 我正在使用rvm来访问我的rails环境。 但是当我使用“rails s”命令启动服务器并使用“http:// localhost:3000”命中url时,请说 – connection not established。

关于Rails中Postgresql的准备声明

现在我正在从SQLite迁移到Postgresql,我遇到了这个问题。 以下准备好的语句适用于SQLite: id = 5 st = ActiveRecord::Base.connection.raw_connection.prepare(“DELETE FROM my_table WHERE id = ?”) st.execute(id) st.close 不幸的是它不能与Postgresql一起使用 – 它在第2行引发了一个例外。我一直在寻找解决方案,并且遇到了这个问题: id = 5 require ‘pg’ conn = PG::Connection.open(:dbname => ‘my_db_development’) conn.prepare(‘statement1’, ‘DELETE FROM my_table WHERE id = $1’) conn.exec_prepared(‘statement1’, [ id ]) 这个在第3行失败。当我打印这样的exception时 rescue => ex ex包含这个 {“connection”:{}} 在命令行中执行SQL是有效的。 知道我做错了什么吗? 提前致谢!

Rails:如何使用scope来查找数组数组中的元素

我有一组数组,如[[“2″,”3”], [“3″,”1”], [“6”, “1”]] 。 每个子arrays的第一个元素是用户ID,第二个元素是用户为事件保留的席位数。 我想让每个用户通过在数组中查找他的ID来查看他的预订。 假设我有两个模型:用户和事件。 在用户控制器中,我想使用像@mybooking = Event.mybooking(current_user.id)这样的范围,问题是如何在事件模型中编写适当的范围? 并且,如果找到用户,我也想使用它的第二个元素。 我尝试了不同的解决方案,但没有奏效! 如果您认为使用示波器是不可能的,并且您有另一种解决方案,请告诉我。 编辑:由于我还在等待一个有效的解决方案,我应该提一下,我正在寻找这样的东西: scope :mybookings, ->(id){where(“reservations.to_enum.map{|n,m| n} ?”, id)} 要么 scope :mybookings, ->(id) { where(“reservations.map(&:first) ?”, id) } 由于与“……”部分相关的错误,这两个不起作用。 并且,下面的解决方案不正确,因为我从用户控制器调用事件的范围,并且不能在该控制器中使用reservations ,因为此变量用于事件控制器。

如何使用ActiveRecord列出所有数据库

我希望能够使用ActiveRecord列出所有数据库。 所以我需要在终端中使用等效于以下命令的ActiveRecord: psql –host 192.168.0.100 –port 5432 –username postgres –list

如何引用并将多个User_ID保存到单个表单并在索引/显示页面Rails 4 App中显示所述ID

嘿所有我正在Rails 4 Ruby 2.2中构建一个CAD应用程序; 在我的通话表单中,我有一个允许调度员选择最多4个单元发送到呼叫的方框。 (见下面的布局)。 这个问题的简短之处在于找出如何保存和访问可在Users模型中找到的4个独立user_id,并将它们存储在Calls模型中,以便它们可以链接并显示在我的Show.html.erb中单位1-4(如果需要所有这些单位) 这是forms: Units Responding Unit #1 Time On Scene Time Clear true } ) %> Unit #2 Time On Scene Time Clear false } ) %> Unit #3 Time On Scene Time Clear false } ) %> Unit #4 Time On Scene Time Clear false } ) %> 我最初将此作为我的collection_select代码: […]

Rails .where()查询不起作用

非常感谢你的帮助。 我有一个locations和ads表。 位置has_many :ads我在位置模型上执行以下查询。 @locations = Location.joins(:ads).where(@location_params.require(:location).permit(:id)).includes(:ads) 然后我想对@locations执行额外的查询并根据以下内容对其进行过滤:ads @locations = @locations.joins(:ads).where(ads: @ads_params.require(:ads).permit(:id)).includes(:ads) 此查询不起作用。 它返回一个空对象。 => # 这是参数: location_params @location_params.require(:location).permit(:id) 1} permitted: true> ads_params @ads_params.require(:ads).permit(:id) 1} permitted: true> 不允许像这样给出特定的列输入: @locations = Location.joins(:ads).where(locations: {id: 1}, ads: {id: 1}) 我有postgresql,我没想过使用SQL,但我不确定。 查询参数 我正在使用rails console来执行此查询,以进行测试。 @location_params => 1} permitted: false>} permitted: false> @location_params.require(:location).permit(:id) => 1} permitted: true> @ads_params => 1} […]

使用BETWEEN时间戳的SQL查询的意外结果

我创建了一个小测试应用程序来追踪我在Heroku上使用Postgres时遇到的问题: http : //snippi.com/s/xd511rf 正如您在第49行中看到的,我想要检索今天创建的所有条目。 这将是我使用Ruby Gem DataMapper测试数据的前两项。 当我在笔记本上运行这个应用程序(Ubuntu 12.10,HP,Ruby 1.9.3)时,我得到了这个结果,这是正确的: [ { “id”: 1, “text”: “Working on some awsomenewss”, “category”: 0, “starttime”: “2013-03-21T15:56:00+01:00”, “endtime”: “2013-03-21T18:26:00+01:00”, “creation”: “2013-03-21T16:15:21+01:00” }, { “id”: 2, “text”: “facebooking”, “category”: 0, “starttime”: “2013-03-21T20:48:00+01:00”, “endtime”: “2013-03-21T22:26:00+01:00”, “creation”: “2013-03-21T16:15:21+01:00” } ] 在我的调试控制台中,将记录此SQL查询: SELECT “id”, “text”, “category”, “starttime”, “endtime”, “creation” FROM “entries” WHERE […]