...s assume a Ruby on Rails application “booksandreviews.com” exists with three models: class Book < ActiveRecord::Base belongs_to :author has_many :reviews end class Author < ActiveRecord::Base has_many :books...
class Review < ActiveRecord::Base belongs_to :book end 1. Nested Queries Active Record’s where method returns an instance of ActiveRecord::Relation. These relations can be passed to other...
You can defined the transaction method on any class that inherits from ActiveRecord::Base, and that transaction will open up a single new database connection. 1. Opening database...
...here is that the transaction can be called on any class that inherits from ActiveRecord::Base. Why is that the key? Well, you might remember that we initially started off...
...result in an infinite callback loop causing SystemStackError: stack level too deep. class User < ActiveRecord::Base has_one :profile, dependent: :destroy end class Profile < ActiveRecord::Base belongs_to :user, dependent...
...of the models cannot be saved. class Registration # ... def save return false if invalid? ActiveRecord::Base.transaction do user = User.create!(email: email, password: password) user.create_location!(country: country, city: city)
...for another model. It also has some virtual attributes and custom validations. class User < ActiveRecord::Base has_secure_password attr_accessor :changing_password, :original_password, :new_password validates_presence_of...
...tokens in our web apps. Here is how we typically build it. class User < ActiveRecord::Base before_create :set_access_token private def set_access_token self.access_token = generate_token...
CreateProjectPolicy.new(current_user, redis) end def redis Redis.current end end class User < ActiveRecord::Base enum role: [:manager, :employee, :guest] end The result is a clean Controller and Model...
...this behavior is defining three types of scopes on your Article model: class Article < ActiveRecord::Base scope :newest, -> { order(updated_at: :desc) } scope :top, -> { order(upvotes_count: :desc) }
...Mar 2016 09:15:30 UTC +00:00 Addition of touch option in ActiveRecord::Base#save In Rails 5, by passing touch: false as an option to save, we can...
...subclass inheritance with the same database table. Just use the following methods: class User < ActiveRecord::Base end class Admin < User def find_sti_class(type_name) type_name = self.name
Best results in other decks
...on database statements by default, the following query will run for an entire day: ActiveRecord::Base.connection.execute("SELECT pg_sleep(86400)") The good news is that with statement_timeout for PostgreSQL...
...adapter: postgresql # ... variables: statement_timeout: 10s # or ms, min, etc Test that it works: ActiveRecord::Base.connection.execute("show statement_timeout;").map { |row| row } => [{"statement_timeout"=>"10s"}] begin ActiveRecord::Base.connection.execute("SELECT pg...
...you have to explicitly tell rails for each transaction to indeed use proper nesting: ActiveRecord::Base.transaction(joinable: false, requires_new: true) do # inner code end This is a safer default...
...this would do meaningful things and only roll back under certain conditions): class Country < ActiveRecord::Base after_save :do_something def do_something raise ActiveRecord::Rollback end end