Skip to content

Instantly share code, notes, and snippets.

@Pserg
Pserg / rails-jsonb-queries
Created November 16, 2022 07:29 — forked from mankind/rails-jsonb-queries
Ruby on Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
class ParamsSlicer
def initialize(params, *whitelist)
@params = params
@nested_whitelist = whitelist.extract_options!
@whitelist = whitelist
end
def call
params.slice(*whitelist).tap do |result|
nested_whitelist.each do |k, v|
@Pserg
Pserg / ruby_operator_precedence.md
Created January 29, 2018 11:09
Ruby Operator Precedence
@Pserg
Pserg / ar_migrate.rb
Created January 13, 2018 14:01 — forked from icyleaf/ar_migrate.rb
ActiveRecord type of integer (tinyint, smallint, mediumint, int, bigint)
# activerecord-3.0.0/lib/active_record/connection_adapters/mysql_adapter.rb
# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)' # compatibility with MySQL default
@Pserg
Pserg / rails_migration_cheatsheet.md
Created December 20, 2017 07:15 — forked from amejiarosario/rails_migration_cheatsheet.md
Rails Migration - Cheatsheet
@Pserg
Pserg / faker_cheat_sheet.md
Created November 29, 2017 04:34 — forked from mikekovacevic/faker_cheat_sheet.md
Faker Ruby Gem Cheatsheet

A cheatsheet of all the Faker Wiki Pages

Faker::Address

  • city
  • city_prefix
  • city_suffix
  • country
  • postcode
  • secondary_address
  • state
@Pserg
Pserg / shoulda_cheat_model.rb
Created November 29, 2017 04:33 — forked from luisalima/shoulda_cheat_model.rb
Shoulda model testing cheat sheet
# updated from the original @ http://cheat.errtheblog.com/s/rspec_shoulda
# just a subset -- models -- is included here. I'll update this, and create cheat sheets for others, as I go along.
# I marked the ones I added with NEW and also added the links to the corresponding code, as I think it's useful.
# Any comments/corrections are welcome!
# ================= Data and Associations =======================
# https://github.com/thoughtbot/shoulda-matchers/tree/master/lib/shoulda/matchers/active_record
it { should_not have_db_column(:admin).of_type(:boolean) }
@Pserg
Pserg / capybara cheat sheet
Created January 1, 2017 12:43 — forked from zhengjia/capybara cheat sheet
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')