Flowdock

Good notes posted by Ariejan

RSS feed
August 7, 2009
7 thanks

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],
November 19, 2008
9 thanks

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”

November 18, 2008
9 thanks

Pop for last, Shift for first

If you want to pop the first element instead of the last one, use shift .

September 9, 2008 - (<= v2.1.0)
4 thanks

Reset a form

To reset a form easily you can do the following:

page["formid"].reset
August 27, 2008
16 thanks

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
August 11, 2008
8 thanks

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.