Skip to content

Instantly share code, notes, and snippets.

@wellsmuker
wellsmuker / rails-jsonb-queries
Created November 23, 2022 12:14 — 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")
@tabishiqbal
tabishiqbal / _form.html.erb
Last active October 24, 2025 19:19
Ruby on Rails Tom-Select Example with Stimulus controller
<%= form_with(model: team) do |form| %>
<div>
<%= form.label :name %>
<%= form.text_field :name, class: "input" %>
</div>
<div>
<%= f.select :user_id, {}, {placeholder: "Select user"}, {class: "w-full", data: { controller: "select", select_url_value: users_path }} %>
</div>
@lanks
lanks / picker-controller.js
Created July 7, 2020 20:50
Date range picker stimulus example
import { Controller } from "stimulus"
import moment from 'moment';
export default class extends Controller {
static targets = [ "date_range" ]
connect() {
if(this.date_rangeTarget) {
let selectedRange = false
$(this.date_rangeTarget).daterangepicker({
showCustomRangeLabel: false,
alwaysShowCalendars: true,
@lorenadl
lorenadl / add_password_expiration_to_devise.md
Last active May 27, 2025 09:17
[RoR] Add password expiration feature to Devise

Add password expiration feature to Devise

Assuming you already have a Devise model named User and you want to add following Devise Security Extension to it:

  • Password Expirable
  • Password Archivable
  • Session Limitable

Add gem and run the generator

@mankind
mankind / rails-jsonb-queries
Last active March 30, 2026 13:03
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")
@somazx
somazx / nested, serialized dataobjects.md
Last active May 3, 2022 14:25
Working with nested, serialized dataobjects in Rails

Rails and serialized nested DataObjects

I wanted to store arbitrary nested data structures in a jsonb column and be able to retrieve and work with that data seemlessly within a Rails application. There are several tutorials covering different aspects of this, but none felt complete to me.

For this example we'll be working with a User model that can define surveys. Each survey consists of n multiple choices questions, and each question can have between two and ten answer options.

Requirements:

In this case we use Postgresql's JSONB support to store the data, but you could also use other forms of serializing such as a text column and Rails' default YAML serializing. JSONB is chosen in this case because it leaves open the possiblity of querying the data with SQL. Also, apparently serializing to JSON is much faster.[1]

@wellsmuker
wellsmuker / strong_params_helpers.rb
Created October 6, 2016 08:19 — forked from BGuimberteau/strong_params_helpers.rb
Strong parameters with grape
module StrongParamsHelpers
extend Grape::API::Helpers
def permitted_params
@permitted_params ||= declared(params, include_missing: false, include_parent_namespaces: false)
end
end
@mlanett
mlanett / rails http status codes
Last active April 3, 2026 06:12
HTTP status code symbols for Rails
HTTP status code symbols for Rails
Thanks to Cody Fauser for this list of HTTP responce codes and their Ruby on Rails symbol mappings.
Status Code Symbol
1xx Informational
100 :continue
101 :switching_protocols
102 :processing
@lfender6445
lfender6445 / gist:9919357
Last active April 10, 2026 14:51
Pry Cheat Sheet

Pry Cheat Sheet

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger

@bigglesrocks
bigglesrocks / README.md
Last active June 16, 2025 21:11
Rails Scoped Invitation System

#Scoped Invitation System for User Groups with Rails#

Starting out with the following models and associations:

####User

  • has_many :memberships
  • has_many :organizations through :memberships

####Organization (User Group)

  • has_many :memberships