activerecord

Unable to use TimescaleDB in Rails test environment

允我心安 提交于 2020-06-01 05:53:50
问题 I'm stuck using TimescaleDB in Rails - everything works fine in development, but in my test suite I cannot insert any data. What I tried A) Use SQL schema dump This causes the original error message I saw. It does create parts of the schema for TimescaleDB but not all of it. I have a hypertable but it's not working properly B) Use Ruby schema dump This lets me insert into my table but it's not a hypertable at all - the ruby syntax looses everything related to TimescaleDB and hypertables. C)

ActiveRecord model.update_column with subquery

北战南征 提交于 2020-05-17 08:49:24
问题 I want to update a column in a Supply model that updates with its stock based on a sum of a subset. The thing is that actually hits two queries to make it works. total_stock = History.sum(:quantity) # first query Supply.update_columns(stock: total_stock) # second query Then, I tried to write a subquery so: total_stock_subquery = mysubset.select(Arel.sql('SUM(quantity)')).to_sql Supply.update_columns(stock: total_stock_subquery) # it should be only one query But, ActiveRecord will replace the

Complex nested aggregations to get order totals - part 2

≯℡__Kan透↙ 提交于 2020-05-17 06:31:04
问题 Follow-up to this answered question: Complex nested aggregations to get order totals After getting it answered I realized that it would be much easier if I start of with: Budget.find(2).cbs_items.order('cbs_items.name ASC').select(:id, :name) Budget Load (0.3ms) SELECT "budgets".* FROM "budgets" WHERE "budgets"."company_id" = 9 AND "budgets"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]] CbsItem Load (1.6ms) SELECT "cbs_items"."id", "cbs_items"."name" FROM "cbs_items" INNER JOIN "cost_breakdown

Do Heroku staging apps share database with review apps?

喜你入骨 提交于 2020-05-16 04:10:15
问题 I've noticed some odd behaviour when working with review apps. I created a review app for a pull request I was working on. It's parent was my staging app. As part of the pull request I migrated the database many times and I was testing those migrations with the review app. I'm working with Rails. I was preparing to merge my changes and deploy to the staging app, but I noticed that the data in my staging app had changed. There have been no deploys to staging since I started this work. For

Do Heroku staging apps share database with review apps?

徘徊边缘 提交于 2020-05-16 04:09:59
问题 I've noticed some odd behaviour when working with review apps. I created a review app for a pull request I was working on. It's parent was my staging app. As part of the pull request I migrated the database many times and I was testing those migrations with the review app. I'm working with Rails. I was preparing to merge my changes and deploy to the staging app, but I noticed that the data in my staging app had changed. There have been no deploys to staging since I started this work. For

Override the default Rails model template

佐手、 提交于 2020-05-13 05:23:38
问题 I want to override the default model file that's generated with rails generate model . I've created a template based on this file, but I can't figure out where to put it. Other answers seem to suggest /lib/templates/rails/model/model.rb or /lib/templates/rails/model/model_generator.rb , but neither of those do anything - I put the template in that location but when I run rails generate model ModelName it gets ignored. Am I going about this the right way? Where should I put the template? 回答1:

Enabling the hstore extension in Amazon RDS from Rails 4

◇◆丶佛笑我妖孽 提交于 2020-05-13 04:27:45
问题 I have a Rails 4 application that uses ActiveRecord to interact with a PostgreSQL 9.3 database. The application makes use of the hstore extension in PostgreSQL for storing key-value pairs in a single database field. Accordingly, Rails automatically detects what I did with that migration and creates the following line in your schema.rb file enable_extension "hstore" In the development and test environments, that works great, as I am deploying to stand-alone PostgreSQL instances. However in

Rails - Active Record: add extra select column to find(:all)

ぐ巨炮叔叔 提交于 2020-05-12 11:36:06
问题 I want to add new column sum(time_entries.hours) to sql select I'm querying entries like this: issues = Issue.visible.where(options[:conditions]).all( :include => ([:status, :project, :time_entries] + (options[:include] || [])).uniq, :conditions => statement, :order => order_option, :joins => query_joins(order_option.join(',')), :limit => options[:limit], :offset => options[:offset], :group => "#{Issue.table_name}.id" ) It generates this select: SELECT "issues"."id" AS t0_r0, ... "time

YII2组件之GridView

余生长醉 提交于 2020-05-05 13:57:02
采用的是yii2.0.14版本,为了学习方便,以问答式书写。 开始GridView GridView主要是为了实现表格复用,尤其我们做后台的时候,你发现表单和表格占据了大部分页面,而表格的样式又是高度的统一,那么如果有这样一个挂件,传入数据集自动渲染表格该多好。 于是GridView出现了,一个有细节、够稳定的表格渲染挂件。 通常情况下GridView是和各种dataProvider配合使用,针对于yii2框架中的dataProvider我之前写过一篇文章,你可以看下,这将有助于你对GridView的学习。 在学习GridView挂件之前,我们需要先了解GridView的结构,看下图。 简单的说,一个GridView由N个column(列)组成,而每个列里有自己的header、content和footer操作,这在GridView的代码中有所体现 try { echo GridView:: widget([ 'dataProvider' => $dataProvider , 'columns'=> [ 'id', 'created_at:datetime:生成时间', [ 'label'=>'会员名', 'attribute'=>'username', 'format'=>'text' ] , ] ]); } catch (\ Exception $e ){ // todo }

小记----Mybatis

北战南征 提交于 2020-05-05 02:33:24
使用mybatis操作数据库有两种方式xml注入和@注解方式,两种方式在项目中是可以共存的。 注解方式:使用注解方式 默认 需要实体类的属性值和表的列名保持一致,否则无法映射到对应的值;或者使用@Results来手动设置映射关系,优点:看着优雅一点,方便修改,缺点:动态sql语句不好写,比较麻烦 xml形式:很常用的方式,有各种逆向工程可以生成mapper.xml文件,看着比较乱,各种标签满天飞,修改的时候很痛苦 我们会发现无论是hibernate还是mybatis,为了方便我们的写sql都煞费苦心。但最后他们都殊途同归指向了“约定大于配置”。hibernate搞了个jpa,而mybatis也相应推出了mp。而mp 是一个 MyBatis 的增强工具 。 看看官网对他的介绍: 无侵入 :只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑 损耗小 :启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作 强大的 CRUD 操作 :内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 支持 Lambda 形式调用 :通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错 支持主键自动生成 :支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence)