June 22nd, 2008
You may have heard of it, but just in case, Living on the Edge is now going to be published on the official Ruby on Rails weblog. Big thanks to Gregg Pollack for getting me the new “gig”, and more importantly, for reviving and freshening up the content on the official Rails blog.
Catch the new first edition - this one’s about the API changes since Rails 2.1.
May 25th, 2008
No mind-blowing changes in Rails this week prior to RailsConf - as Gregg mentioned last week in the Rails Envy podcast, it’s pre-2.1 days (Rails 2.1 will probably be released at RailsConf) so it’s pretty easy to see why. Oh and all Rails tests now pass in Ruby 1.9 after a long-standing #module_eval bug got fixed in Ruby 1.9’s trunk (see thread for more details).
I’ll be at (my first) Railsconf 2008 in Portland, Oregon this Thursday onwards - if anyone sees me and recognizes me from my Facebook picture please come and say hi (no head butting though).
This week’s report covers changes from 19th May 2008 to 25th May 2008 (the day the corresponding Rails Envy podcast was recorded).
first and last methods now work with associations and named_scope
Remember how the merging of the has_finder gem into Rails allowed you to do things like Post.first and Post.last?
Now you can go one step further and use the same methods on your ActiveRecord associations. For example:
post = Post.find(1)
first_comment = post.comments.first
If you have a named_scope named recent defined, you can even do this:
post.comments.recent.last
This neat little enhancement is courtesy of Ryan Bates (yes, that Ryan Bates of Railscasts fame).
Related changeset: http://github.com/rails/rails/commit/73c59638549686fccc749ffd3ac53cb533c5fd61
Cache stores now have an exist? method and controllers get fragment_exist?
The cache stores in Rails (Memcache, file stores, etc.) now have an exist? method that checks whether a cached value exists given a cache key. This allows Rails controllers to expose a fragment_exist?
method that allows you to check for existence of a cache fragment:
fragment_exist?('example.com/foo/bar')
This little enhancement is courtesy of José Valim.
Related changeset: http://github.com/rails/rails/commit/99860b72aebe0348f41e82d4710343498d89a84b#diff-2
Create association records with block argument
You can now create records for associations like so:
post.coments.create!(:title => 'Techcrunch') do |c|
c.body = "Rails can't scale"
end
This is in keeping with the ActiveRecord::Base.create change previously mentioned.
Credit for this patch goes (once again) to Ryan Bates.
Related changeset: http://github.com/rails/rails/commit/6cba97d2a449faf21aec9fe9d4434067e414226f
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.
May 18th, 2008
It’s another slow week (just 2 changes of note imho) after the release of the 1st Release Candidate (RC1) of Rails 2.1. Follow that link for installation instructions - though if you’re reading this blog post you probably don’t care! (because you’re, you know, “living on the edge”). Cheesiness aside, be sure to report any bugs you may encounter when upgrading to 2.1 RC1 or edge at the Rails bug tracker - it is an RC so any bug reports would be very welcome and useful!
This week’s report covers changes from 12th May 2008 to 18th May 2008 (the day the corresponding Rails Envy podcast was recorded).
caches_action can has conditionals
caches_action now takes an :if option (just like caches_page does). For example:
caches_action :index, :if => Proc.new { |c| !c.request.format.json? }
This little enhancement is courtesy of José Valim.
Related changeset: http://github.com/rails/rails/commit/7708650f73ddb4db300ea2059c60c1d907a4384e
Bugfix: :select option is now scanned in ActiveRecord finders to ensure needed tables are included in generated SQL
Post.find(:all, :include => :author, :select => 'posts.*, authors.id as "author_id"', :limit => 2)
Would generate an SQL statement like this:
SELECT posts.*, authors.id as "author_id" FROM "posts" LIMIT 2
Notice how the authors table is not joined. This oversight is now fixed.
Thanks go to John Devine for this bugfix.
Related changeset: http://github.com/rails/rails/commit/b28b54cab090bed8f099ef375b419a8f92390dd4
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series. Also, if anyone has any recommendations of (non-ruby-related) things to do in Portland, do let me know!
May 4th, 2008
This week’s report covers changes from 29 April 2008 to 4th May 2008 (the day the corresponding Rails Envy podcast was recorded).
change_table for ActiveRecord migrations
Thanks to Jeff Dean, who also blogged about the new change_table feature in ActiveRecord migrations, you can now change a table with a block like so:
change_table :videos do |t|
t.add_timestamps
t.add_belongs_to :goat
t.add_string :name, :email, :limit => 20
t.remove_column :name, :email # takes multiple arguments
t.rename :new_name
t.string :new_string_column # executes against the renamed table name
end
Some key things to note:
add_XXX would add a new column for you, e.g. add_string would add a new string field.
- Of course, add_timestamps would add the magic
created_at and updated_at datetime fields.
remove_column now takes multiple arguments.
rename would rename the table.
Very nice, DRY enhancement, props to Jeff Dean once again.
Related changeset: http://github.com/rails/rails/commit/96980bd561d79824b6cb6efbcbecdcbf8785d452
ActiveRecord::Base.create takes a block like ActiveRecord::Base.new
Yup now you can also create ActiveRecord objects with a block argument just like you could for ActiveRecord::Base.new:
@person = Person.create(params[:person]) do |p|
p.name = 'Konata Izumi'
p.age = 17
end
Credit goes to Adam Meehan for this patch.
Related changeset: http://github.com/rails/rails/commit/dd120ede53eaf71dee76894998a81626b7a689fc
Bugfix: change_column should be able to use :null => true on a field that
formerly had false
You can now use change_column in your migrations to alter a column as nullable if it was previously NOT NULL.
This bugfix is courtesy of Nate Wiger.
Related changeset: http://github.com/rails/rails/commit/10ef65a3b054270ed3d458ec8eb7c2b9a3e638f7
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.
April 27th, 2008
This week’s report covers changes from 21 Apr 2008 to 27 Apr 2008 (the day the corresponding Rails Envy podcast was recorded).
Not much interesting to report this week - there were mostly a bunch of bugfixes and Ruby 1.8.7-compatibility commits.
Introduce ActiveResource::Base.timeout and rescuing from Timeout::Error in ActiveResource::Connection
These are 2 changes that I’d talked about earlier this week so I won’t repeat myself - take a read through ActiveResource timeouts and why it matters.
Smart integer datatype for the MySQL adapter in migrations
The MySQL adapter in Rails now maps the integer column type in your migrations to either smallint, int, or bigint depending on the :limit option.
This means that a migration like this:
def self.up
create_table :searches do |t|
t.integer :foo, :limit => 2
end
will create your foo column as a smallint(2) MySQL datatype (instead of int(2) before). (More information MySQL numeric datatypes.)
Credit goes to DHH for this patch.
Related changeset: http://github.com/rails/rails/commit/a37546517dad9f6d9a7de6e1dba4d960909d71e8
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.