Tag: sql

MySQL或Rails在特定日期范围内每天获得AVG的最佳方式

我正在尝试在Rails中制作图表,例如在给定日期范围内每天的平均销售额 假设我有一个product_sold模型,它具有“sales_price”浮点属性。 但是如果特定日期没有销售(例如模型/ db中没有),我想简单地返回0。 MySQL / Rails完成这项工作的最佳方法是什么? 我知道我可以这样做: ( 这个SQL查询可能是完全错误的方式来获得我想要的东西 ) SELECT avg(sales_price) AS avg, DATE_FORMAT(created_at, ‘%m-%d-%Y’) AS date FROM products_sold WHERE merchant_id = 1 GROUP BY date; 得到这样的结果: | 平均| 日期| 23 01-03-2009 50 01-05-2009 34 01-07-2009 …… 我想得到的是: | 平均| 日期| 23 01-03-2009 0 01-04-2009 50 01-05-2009 0 01-06-2009 34 01-07-2009 0 01-08-2009 […]

关于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 .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 […]

使用Chef运行所有sql文件

你好我需要帮助我做了一个命令,应该读取sql_dumps文件夹里面的所有sql文件,但它不工作..这是我得到的。 execute “Run_SQL_Files” do dirResults = Dir.glob(“/tmp/sql_dumps/*.sql”) var = 0 while var < 15 do var = var + 1 command "mysql –user=root –password=toomba source" + dirResults[var] # Already tried this also # command "mysql –user=root –password=toomba < " dirResults[var] puts dirResults[var] end end 我不太熟悉ruby。 这是我收到的错误 default: Errno::ENOENT default: ————- default: No such file or […]

无法在Rails应用程序中查找和显示关系模型中的记录列表

我有一个关系模型, has_many :through关联跟踪has_many :through中商店的客户(用户)。 我在使用ShopsController遇到问题并查找给定商店的关系列表,然后显示它们。 我的错误是RecordNotFound, Couldn’t find Relationship with ‘id’=所以我遇到了如何创建和识别关系的问题。 当商店登录时,需要根据user_id找到它们(然后给出current_shop_id )我如何重新设计这些以使关系/展示工作? 在当前版本中,商店通过关系拥有多个用户: class Shop < ActiveRecord::Base has_many :relationships has_many :users, through: :relationships Relationships获取用户的id和shop的id并创建一个关系id: schema.rb create_table “relationships”, force: :cascade do |t| t.integer “user_id” t.integer “shop_id” t.datetime “created_at”, null: false t.datetime “updated_at”, null: false end add_index “relationships”, [“shop_id”], name: “index_relationships_on_shop_id” add_index “relationships”, [“user_id”, “shop_id”], name: […]

SQLite3“忘记”使用外键

我正在使用Ruby和SQLite3,但我在Sqlite3中使用外键的尝试很不幸。 根据sqlite3 –version ,安装了3.7.13版。 据我所知,自3.6.x版以来,Sqlite3支持外键。 我知道默认情况下外键被禁用,必须使用PRAGMA foreign_keys = ON;激活PRAGMA foreign_keys = ON; 。 在我的Ruby db-create-script中,我正在做这样的事情: sql = <<-SQL PRAGMA foreign_keys = ON; CREATE TABLE apps ( id …. ); CREATE TABLE requests ( … app_id INTEGER NOT NULL, FOREIGN KEY(app_id) REFERENCES apps(id), ); … SQL db.execute_batch(sql) 不幸的是,我可以愉快地将行插入到具有未知app-id的requests ,它可以工作,但当然不应该。 有意思:直接使用sqlite3 shell,我可以观察到以下行为: $ sqlite3 database.db sqlite> PRAGMA […]

独特的加入Rails

video有很多活动 我正在努力获取将来安排活动的所有video。 我已经有了这个: named_scope :scheduled_in_future, :joins => :event, :conditions => [“event.scheduled_start > ? AND event.status = ?”, Time.now.to_i, ‘PENDING’] 这是有效的,但如果同一个video将来有多个事件,它将给我重复的video记录。 当然,我可以通过数组并清除重复项,但必须有一种SQL方法来完成它。 我试过添加一个 :select => “DISTINCT(video.id)” 但它只返回ID字段而不是整个记录。

在rails上连接Ruby中的Sql server

我是ruby on rails上的ruby on rails新手。 我想知道如何将Sql server连接到rails应用程序。 我在Windows环境中使用RoR。 是否有任何链接在ROR中设置与SQL Server连接?

有人知道Ruby SQL解析器吗?

有人知道Ruby SQL解析器吗?