rails-activerecord

Group activerecord relation

家住魔仙堡 提交于 2019-12-24 03:59:28
问题 After a select on a table I get a result: [#<Result url: "http://www.url.com/1", key_id: 1>, #<Result url: "http://www.url.com/1", key_id: 2> [...] How can I group the url and keep the key_id , something like: ["http://www.url.com/1", [1, 2]]=>2 Thank you 回答1: Here's the code: grouped_results = Result.all.group_by(&:url).map do |url, results| { [url, results.map(&:key_id)] => results.count } end You need to replace Result.all by your actual scope or something. 来源: https://stackoverflow.com

Multiple LIKE and AND operators in RAILS/POSTGRESQL

♀尐吖头ヾ 提交于 2019-12-24 01:54:27
问题 I'm using Rails 5.0.1 and Postgresql as my database. I have a table with column :content which contains words. The problem: When I'm looking for a specific word, I want to see if the word contains letters (chars) of my choice. Let's say i want to DB to return words containg letters "a", "b" and "c" (all of them, but with no specific order) What I'm doing: I found that i could use Word.where("content like ?", "%a%").where("content like ?", "%b%").where("content like ?", "%c%") OR Word.where(

Multiple LIKE and AND operators in RAILS/POSTGRESQL

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:54:03
问题 I'm using Rails 5.0.1 and Postgresql as my database. I have a table with column :content which contains words. The problem: When I'm looking for a specific word, I want to see if the word contains letters (chars) of my choice. Let's say i want to DB to return words containg letters "a", "b" and "c" (all of them, but with no specific order) What I'm doing: I found that i could use Word.where("content like ?", "%a%").where("content like ?", "%b%").where("content like ?", "%c%") OR Word.where(

How to listen for database changes triggered by a 3rd party application

落花浮王杯 提交于 2019-12-24 01:45:38
问题 I need to integrate a Ruby on Rails application with a 3rd-party application, which will share a common PostgreSQL database. That is, both the Rails app and the 3rd-party app will be using the same PG database. Is it possible to set a listener within the Rails app, such that an event is fired when the 3rd-party app adds, edits or deletes a database record? For example, say I have a Book model defined in Rails, and the 3rd-party app adds a new row in the database's Book table (not via the

How to sort results based on join table in Rails?

女生的网名这么多〃 提交于 2019-12-24 01:18:37
问题 I have Account model, class Account < ActiveRecord::Base has_many :account_games def score account_games.map(&:score).sum end end And AccountGame : class AccountGame < ActiveRecord::Base belongs_to :account end What is the best way to get accounts with the highest score? as you can see, score is the summation of related account_games score field. Thanks 回答1: try Account.joins(:account_games).sum('account_games.score', group: :account_id, order: 'sum_account_games_score') which will give you a

Affected rows for ActiveRecord::Base.connection.execute with Postgres

倖福魔咒の 提交于 2019-12-24 00:46:35
问题 Is there a way to get the number of affected rows resulting from a SQL operation using ActiveRecord::Base.connection.execute ? I found this answer for MySQL adapters, but it doesn't work with Postgres. Alternatively, if there's a way to get the SQL text response (e.g. "UPDATE 126"), that would work too. 回答1: You can use cmd_tuples method: sql = "UPDATE users SET updated_at = '#{DateTime.now}' WHERE id = 1" ActiveRecord::Base.connection.execute(sql).cmd_tuples # => 1 Documentation: http://www

column_types is nil in Rails 4.0.1

依然范特西╮ 提交于 2019-12-23 16:10:34
问题 I'm attempting to upgrade Rails from 4.0.0 to 4.0.2, but there's a change that was introduced in 4.0.1 that's breaking my application. Here's the error I'm seeing: undefined method `[]' for nil:NilClass activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:86:in `block in read_attribute' activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `fetch' activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `read_attribute' activerecord (4.0.2) lib

Rails 5.2 and Active Record migration with CURRENT_TIMESTAMP

霸气de小男生 提交于 2019-12-23 14:56:59
问题 I have some attributes that need to have default values. I've set up my migrations to set the defaults in the database as follows: class AddDefaultsToModel < ActiveRecord::Migration[5.2] def change change_column :posts, :post_type, :string, default: 'draft' change_column :posts, :date_time, :datetime, default: -> { 'CURRENT_TIMESTAMP' } end end The defaults work great when adding directly to the database. However, if I build a new model in Rails, one attribute works as expected, the other

Rails add two scope with active record result

这一生的挚爱 提交于 2019-12-23 12:58:30
问题 I am using acts-as-taggable-on gem i use single field to search by tag_name and user_name User.rb class User < ActiveRecord::Base acts_as_taggable attr_accessor: :user_name, :age, :country, tag_list scope :tagged_with, lambda { |tag| { :joins => "INNER JOIN taggings ON taggings.taggable_id = user.id\ INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = 'User'", :conditions => ["tags.name = ?", tag], :order => 'id ASC' } } def self.search(search) if search where('name LIKE

Manually updating attributes mounted by Carrierwave Uploader

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 09:27:52
问题 I am unable to use model.update_attribute on an attribute that is mounted by a carrierwave uploader. The SQL statement wont accept the value and adds NULL to the placeholder. If I remove the mount_uploader statement from the model class it works as normal. I am troubleshooting things from the console and trying to add some attributes while seeding the DB and this is thwarting my efforts. Ideas? Thanks. Update: Relevant code: class Profile < ActiveRecord::Base belongs_to :user has_and_belongs