RubyMine not recognizing cucumber features

If RubyMine isn’t recognizing cucumber features, ensure the following:

Cucumber gem is attached to project: RM | Settings | Ruby SDK and Gems | Attach gems.
If you edited *.feature files in early version of RubyMine it may be mapped on Text file. So you should check that *.feature extension is mapped on “Cucumber scenario files” in [...]

How to stop ActiveRecord putting attribute name in validation message

Use http://github.com/gumayunov/custom-err-msg/tree/master
Put a ^ at the start of the :message => option:
Non-base messages are prefixed with the attribute name as usual UNLESS they begin with ‘^’
in which case the attribute name is omitted.

Paperclip/ImageMagick image geometry specifiers

This can be a pain to find so here is the quick access version:
<width>x<height><specifier>
% Interpret width and height as a percentage of the current size.
! Resize to width and height exactly, loosing original aspect ratio.
< Resize only if the image is smaller than the geometry specification.
> Resize only if the image is greater than the [...]

Switching an ActiveRecord instance between STI classes

Sometimes you wish to convert one STI class to another, e.g. promote User to Administrator.
ActiveRecord::Base#becomes(Klass) returns an instance of the current record converted to the specified Klass, e.g.:

 
user = User.find_by_login("sam")
user.becomes(Administrator)

Note however that this does not change the type attribute of the instance, so you still have an additional step to do if you want this [...]

SV-Ruby Meetup, 2009-03-19

Yehuda Katz talked about Rails 3
Rails API should not change! It’s a mistake to change the API and the implementation at the same time (think Ruby 1.9 language and VM).
ORM Agnisticism
With for_for @obj, @obj can be an ActiveRecord, DataMapper, CouchDB, SimpleDB, Sequel object.  Two interfaces are now support for ORM objects, ActiveRecord/DataMapper style: errors, #each, [...]

ActiveRecord performance: validation and associations

If you have two models associated in some way, it is usual to add a validates_presence_of validator to ensure that the models are connected before saving. However, be aware that there are two ways of doing this with different performance ramifications:

class BlogEntry < ActiveRecord::Base
belongs_to :blog
validate_presence_of :blog
end

During validation, Validates_presence_of :blog causes ActiveRecord [...]

Testing Rails routes

The other thing I can never remember off the top of my head:

1
2
3
4
5
6
7
8
9
>> r = ActionController::Routing::Routes
 
>> r.recognize_path ‘/perspective/edit_wizard/1′, :method => :get
 
> {:action=>"edit_wizard", :id=>"1", :controller=>"perspective"}
 
>> r.generate :action=> "edit_wizard", :id=> "1", :controller=> "perspective"
 
> "/perspective/edit_wizard/1"

ActionController::Base.helpers proxy

Tidbit from RailsCast 132:
ActionController::Base.helpers is a proxy object you can use to access Rails view helpers, outside of views, e.g.:

1
ActionController::Base.helpers.pulralize(products.count, ‘product’)

RSpec view tests and url_for

Ran into a problem with RSpec when trying to create a spec for a view that used an ActionView url helper that required additional parameters to generate the url.

route.edit_page ‘:site/edit’, :conditions => { :method => :get }, :controller => ‘webpages’, :action => ‘edit_page’

The above route sets params[:site] which is later used for url generation (using, [...]

ActiveRecord eager-loading thru multiple associations

I just found out how to do something I have been wondering for a long time: :include syntax to eager-load through multiple associations. In the rdoc for class ActiveRecord::Associations::ClassMethods I discovered you can nest include statements in hashes as follows: If for example table_a has_many table_b and table_b has_many table_c and table_c has_many table_d:

Model.find :first, :include => [...]