Notes posted by Ariejan
RSS feedTesting Regular Expressions?
Writing regular expressions is never easy. You may use http://rubular.com to test and optimize your regular expressions.
Join multiple tables
It’s easy to join multiple tables too. In this case we have:
class Article belongs_to :feed end class Feed has_many :articles belongs_to :source end class Source has_many :feeds # t.bool :visible end
You can search articles and specify a condition on the sources table.
Article.find(:all, :conditions => { :feeds => { :sources => { :visible => true }}}, :joins => [:feed => :source],
Formatting options
Readable strftime
%a - The abbreviated weekday name (“Sun”)
%A - The full weekday name (“Sunday”)
%b - The abbreviated month name (“Jan”)
%B - The full month name (“January”)
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (“AM” or “PM”)
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name %% - Literal “%’’ character t = Time.now t.strftime(“Printed on %m/%d/%Y”) #=> “Printed on 04/09/2003” t.strftime(“at %I:%M%p”) #=> “at 08:56AM”
Pop for last, Shift for first
If you want to pop the first element instead of the last one, use shift .
Using your model's connection
It’s possible to execute raw SQL over the currently established connection for a model.
You may configure Rails to use different databases for different models. To make sure you are querying the correct database you may do the following:
MyModel.connection.execute("UPDATE `my_models` SET `beer`='free' WHERE 1")
Only attr_accessible attributes will be updated
If your model specified attr_accessible attributes, only those attributes will be updated.
Use attr_accessible to prevent mass assignment (by users) of attributes that should not be editable by a user. Mass assignment is used in create and update methods of your standard controller.
For a normal user account, for example, you only want login and password to be editable by a user. It should not be possible to change the status attribute through mass assignment.
class User < ActiveRecord::Base attr_accessible :login, :password end
So, doing the following will merrily return true, but will not update the status attribute.
@user.update_attributes(:status => 'active')
If you want to update the status attribute, you should assign it separately.
@user.status = 'active' save
Validations
update_attribute will not perform validations checks when the Validation module is included.
If you want to perform validations when updating, use update_attributes instead.