rails cache



  • difference between caches_page and caches_action: the former just caches the page content to a file, the latter called some before filters in the action before do the cache ( the action itself won’t be called, just its before filters).
  • expire_page, expire_action
  • config.action_controller.perform_caching = true ( be careful if you are in development env; it will do the cache and your action change will NOT have effect immediately. I would set it to be false in development env.)

select and pluck

For example, to select only id columns:

The SQL query used by this find call will be somewhat like:
SELECT id FROM clients

Be careful because this also means you’re initializing a model object with only the fields that you’ve selected. If you attempt to access a field that is not in the initialized record you’ll receive:


a = Person.select(:name).uniq
=> [#<Person name: “Mary”>, #<People name: “John”>]
irb(main):011:0> a.first.name
=> “Mary”
irb(main):012:0> a.first.address
ActiveModel::MissingAttributeError: missing attribute: address

pluck can be used to query a single column from the underlying table of a model. It accepts a column name as argument and returns an array of values of the specified column with the corresponding data type.

select returns a Model object with only :column populated
pluck only returns the values
select(:id) will return an array of Model
pluck(:id) will return an array of integers ( suppose the column id has integer type)

rails console reload

Since you don’t want to restart your console every time to pick up the changes, you can do

  • Load only one file that you made change to, in the console, type

load ‘app/model/your_file_name.rb’

  • load all changes


You will need to call the reload! method in the console to reload the changes. This method’s magic is automatically called by rails server in development mode.

As a comment’s pointed out beneath and another answer here, if you change things to do with the environment of the application, such as adding new gems to the Gemfile, making changes to anything in config or adding a new plugin then you’ll need to restart the console. Any changes to app will be reloadable with reload!

If you were using this particular way to test that a method was working, I wouldn’t. Tests (as in, the Test::Unit or RSpec) variants are much nicer because you have a reproducible way of running them again and again. rails console is great for one-off testing, but if you want to write a maintainable application then write tests.

load is different from reset

db:migrate runs (single) migrations that have not run yet.
db:create creates the database
db:drop deletes the database

db:schema:load creates tables and columns within the (existing) database following schema.rb

db:setup does db:create, db:schema:load, db:seed
db:reset does db:drop, db:setup

object relational mapping

  • Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. ( DB data map to object, object map to DB)
  • active record: ORM for rails
  • Data management tasks in object-oriented (OO) programming are typically implemented by manipulating objects that are almost always non-scalar values. For example, consider an address book entry that represents a single person along with zero or more phone numbers and zero or more addresses. This could be modeled in an object-oriented implementation by a “Person object” with attributes/fields to hold each data item that the entry comprises: the person’s name, a list of phone numbers, and a list of addresses. The list of phone numbers would itself contain “PhoneNumber objects” and so on. The address book entry is treated as a single object by the programming language (it can be referenced by a single variable containing a pointer to the object, for instance). Various methods can be associated with the object, such as a method to return the preferred phone number, the home address, and so on.However, many popular database products such as structured query language database management systems (SQL DBMS) can only store and manipulate scalar values such as integers and strings organized within tables. The programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), or only use simple scalar values within the program. Object-relational mapping is used to implement the first approach
  • Another solution is to use an object-oriented database management system (OODBMS) or document-oriented databases such as native XML databases. OODBMSs are databases designed specifically for working with object-oriented values. Using an OODBMS eliminates the need for converting data to and from its SQL form, as the data is stored in its original object representation and relationships are directly represented, rather than requiring join tables/operations.