Skip to content

Instantly share code, notes, and snippets.

# The models
class TextMessage < ApplicationRecord
belongs_to :account, inverse_of: :text_messages, optional: true
belongs_to :user, inverse_of: :text_messages, optional: true
has_many :phones, ->(text_message) { joins(name: :products).where("products.account_id = ?", text_message.account_id) }, foreign_key: :number, primary_key: :client_phone_number
has_many :names, through: :phones, source: :name
class Phone < ApplicationRecord
belongs_to :name, inverse_of: :phones
Version4::Foo.last.bars.first.class
=> Version4::Bar
Version5::Foo.last.bars.first.class
=> Version4::Bar
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
class ArticleChannel < ApplicationCable::Channel
def subscribed
@article = Article.find(params[:article_id])
@editor_id = params[:editor_id]
# Write it only if the editor is truly nil in a single query to avoid race conditions
Article.where(id: @article.id, editor: nil).update(editor: @editor_id)
stream_for @article