…, I simply grew accustomed to it. An interesting blog post I found recently, Five ActiveRecord Tips , pointed out the :select parameter which I had never seen.
The idea behind :select is that you supply a SQL string which represents the attributes of the objects you want to fetch (or columns in the row if you're me and old school). Let's say that you want to get only the id of the object, the created_at and the updated_at in a table called apps then your :select would …
Use an ActiveRecord model to start the data
Use created_at as a tool to manage the cache expiration
Serialize the data after fetch to store it away
Write a get_latest method inside the model to test whether or not to fetch the data from the cache or the source
The first real problem came from needing to deal with not just straight ActiveRecord ( AR) objects but will_paginate collections that wrap around the AR objects. Here's something brilliant about ActiveRecord, irrelevant …
Avoid legacy ActiveRecord:: Base#find([:all | :first | :last]) statements
You might have heard about the changes in the Active Record query interface for Rails 3 . Here's an interesting quote.
Currently ActiveRecord provides the following finder methods:
find(id_or_array_of_ids, options)
find(:first, options)
find(:all, options)
first(options)
all(options)
update_all(updates, conditions, options)
and the following calculation methods:
count(column, options) …
…document based database, like MongoDB, these would obviously be embedded documents. With ActiveRecord you can use the composed_of -method . Allow me to demonstrate that: # Attributes of Person include: # # * first_name string # * name_infix string # * last_name string # * male boolean # class Person < ActiveRecord :: Base composed_of :name , :mapper => Name .members composed_of :gender , :mapper => Gender .members end class Name < Struct .new( :first_name …
Showing Queries in ActiveRecord 3
As you can see, the queries done by ActiveRecord are displayed in the same way as they are displayed in your log files. In Rails 2, you would've done this by redirecting the log output to STDOUT . In Rails 3 you need to subscribe to the ‘ sql.active_record ‘-notifications.
This could in theory also be done for other Rails 3 compatible ORMs like Mongoid, but I haven't looked into that yet.
Hirb
Hirb formats objects …
…that row until the session that obtained the lock concludes its transaction (COMMIT or ROLLBACK). ActiveRecord allows us to do this using the :lock flag: def ship PurchaseOrder.transaction do @ order = PurchaseOrder.find(params[:id], :lock => true) @order.ship!end redirect_to order_path(@order)end Working through the above example again, the first process to execute the find will issue the following SQL: SELECT * FROM purchase_orders WHERE id = 1 FOR UPDATE Notice the "FOR …
class Post < ActiveRecord:Base
def publish_new_post ( params, user )
post = Self . new ( params )
post. user = current_user
post. expiring_date = Time . now + 7 . day
post. save
end
end
class PostController < ApplicationController
def create
@ post = Post. publish_new_post ( params [ :post ] , current user )
@ post . save
end
end
Once again we managed to write a better code in the Controller by moving the logic in the Model.
This …
There are 12ish adapters for ActiveRecord, but there are tons of JDBC adapters. JRuby to the rescue. @ mokolabs # rubymidwest
RT: @ ajsharp " ALTER TABLE mytable CONVERT TO CHARACTER SET utf8" # rubymidwest That last line is a particularly useful bit of MySQL that will help you convert to UTF8.
Next up is John Hwang (@tavon) with " Object Oriented Unobtrusive CSS" at # rubymidwest
Rename! the talk is now just " Unobtrusive CSS" …
Sometime in rails 2.x the # inspect method for an ActiveRecord class was changed to show you all the column names (attributes) of that class. This is fine when things are small but if your working on a big legacy schema and you want clean terse debugging, all those column names can be noisy. I just set this initializer up today to kill it. Now the class just shows the number of columns. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 module ActiveRecord class Base class << self def …
…ActiveRecord::Base is not the one that implements where() and friends, in fact, it simply delegates to an ActiveRecord::Relation object. From ActiveRecord::Base source code :
delegate :select , :group , :order , :limit , :joins , :where , :preload , :eager_load , :includes , :from , :lock , :readonly , :having , :create_with , :to => :scoped
And the scoped implementation is shown below:
def scoped ( options = nil ) if options. present ? scoped. apply_finder_options …