Ian Jones Logo

Rails

Model

find_in_batches

Documentation URL

Person.where("age > 21").find_in_batches do |group|
  sleep(50) # Make sure it doesn't get too crowded in there!
  group.each { |person| person.party_all_night! }
end

update_column

Documentation URL

Used to skip validations and updating updated_on.

Updates the attributes directly in the database issuing an UPDATE SQL statement and sets them in the receiver:

user.update_columns(last_request_at: Time.current)

This is the fastest way to update attributes because it goes straight to the database, but take into account that in consequence the regular update procedures are totally bypassed. In particular:

Validations are skipped.

Callbacks are skipped.

updated_at/updated_on are not updated.

However, attributes are serialized with the same rules as ActiveRecord::Relation#update_all

This method raises an ActiveRecord::ActiveRecordError when called on new objects, or when at least one of the attributes is marked as readonly.

where

Greater/less than query

User.where("id > ?", 200)

Controller

Params

Nested Strong Params

params.permit(:site_name, [data: {object: {metadata: [:client_id]}}])

This will allow params that look like this:

{
  "site_name": "some site",
  "data": {
    "object": {
      "metadata": {
        "client_id": "some id"
      }
    }
  }
}

CLI

Generate

Model

Attributes default to strings.

rails g model user email age:integer

New

with postgres db

rails new myapp --database=postgres

Subdomains

How do I resolve a subdomain on my local machine?

Stack overflow link

You can use xip.io to alias subdomains your your local machine.

Backlinks