Notes posted by szeryf
RSS feedPotentially slow operation
Remember that checking for a value is a potentially slow operation (all the elements might be iterated) as oposed to querying a key (e.g. with has_key?), which is supposed to be fast in a Hash.
Potentially slow operation
Remember that checking for a value is a potentially slow operation (all the elements might be iterated) as oposed to querying a key (e.g. with has_key?), which is supposed to be fast in a Hash.
File class documentation
Most of the File class documentation is located in IO class docs. What you see here is what ‘ftools’ gives you.
Always a String
Remember that even if you ask for one field that is a number, a String will be returned:
Time.now.strftime("%j") #=> "055"
If you want to avoid SQL...
…you’re probably looking for http://apidock.com/rails/ActiveRecord/Calculations/ClassMethods/count
Security issue with non-HTML formats
Please note that using default to_xml or to_json methods can lead to security holes, as these method expose all attributes of your model by default, including salt, crypted_password, permissions, status or whatever you might have.
You might want to override these methods in your models, e.g.:
def to_xml super( :only => [ :login, :first_name, :last_name ] ) end
Or consider not using responds_to at all, if you only want to provide HTML.
Easier way
See http://apidock.com/rails/ActionController/TestProcess/fixture_file_upload for easier way to test file uploads.
Output format
This outputs date & time in format of yyyy-MM-ddThh-mm-ssZZZ. All values are preceded with 0 if less than 10. Hours are in 0..23 range. Timezone is sticked at the end. Watch out for capital T in the middle :)
Can be used on classes, too
For example:
class C protected def foo end end p C.protected_instance_methods(false)
outputs:
["foo"]
Documentation
Good docs can be found here: http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api
See also: http://rails-i18n.org/wiki for an extensive list of resources.
Deprecated
This method is deprecated. You should use:
I18n.translate('activerecord.errors.messages')
Possible gotcha
Please note that exists? doesn’t hold all the conventions of find, i.e. you can’t do:
Person.exists?(:conditions => ['name LIKE ?', "%#{query}%"]) # DOESN'T WORK!
You can't use Symbols, but you can use Regexps
You can’t use Symbol (although Symbol is accepted with render :action => :new), like:
assert_template :new # WON'T WORK!
But you can use Regexp, e.g.:
assert_template /new/ # WORKS OK
Note that the String matched with your Regexp is the full path to the template relative to the view/ directory of your app, so this will not work:
assert_template /^new$/ # WON'T WORK!
However this might:
assert_template /^employees\/new.html.haml$/