Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active April 12, 2022 01:10
Show Gist options
  • Save JoshCheek/44e18707dfdf630bcb30dd78962bfd06 to your computer and use it in GitHub Desktop.
Save JoshCheek/44e18707dfdf630bcb30dd78962bfd06 to your computer and use it in GitHub Desktop.
ActiveRecord deprecation... so how are you supposed to write this, then?
gem 'activerecord', '= 6.0.4.6'
require 'active_record'
ActiveRecord::VERSION::STRING # => "6.0.4.6"
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table(:users) { |t|
t.string :user_name
t.string :first_name
t.string :last_name
}
end
User = Class.new(ActiveRecord::Base) { has_many :posts }
User.create! user_name: 'Just Joshin', first_name: 'Josh', last_name: 'Cheek'
User.create! first_name: 'Jane', last_name: 'Doe'
User.create! first_name: 'Jenn'
User.create! last_name: 'Smith'
User.pluck(:id, "coalesce(user_name, first_name||' '||last_name, first_name, last_name)")
# => [[1, "Just Joshin"], [2, "Jane Doe"], [3, "Jenn"], [4, "Smith"]]
# !> DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "coalesce(user_name, first_name||' '||last_name, first_name, last_name)". Non-attribute arguments will be disallowed in Rails 6.1. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). (called from <main> at program.rb:21)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment