Flowdock

Recent notes

RSS feed
March 27, 2012
4 thanks

Makes it possible to use a scope through an association

This is a very useful method if you want to to use a scope through an association:

class Book < ActiveRecord::Base
  scope :available, where(:available => true)
end

class Author < ActiveRecord::Base
  has_many :books
  scope :with_available_books, joins(:books).merge(Book.available)
end

# Return all authors with at least one available book:
Author.with_available_books

See http://asciicasts.com/episodes/215-advanced-queries-in-rails-3 for more info.

March 27, 2012
7 thanks

Reorder

If you want to override previously set order (even through default_scope), use reorder() instead.

E.g.

User.order('id ASC').reorder('name DESC')

would ignore ordering by id completely

March 27, 2012 - (>= v3.2.1)
0 thanks

Example to auto download

Controller

op = Operation.find(params[:id])
fname = "operation_#{op.id}_#{DateTime.now.to_i}.csv"
send_data op.export(params[:url_type]), 
  :type => 'text/csv; charset=iso-8859-1; header=present',
  :disposition => "attachment; filename=#{fname}.csv"

export_csv

def export(url_type)
  csv_data = CSV.generate do |csv|
    csv << self.header_columns # simple array ["id","name"]
    url_items = @operation.url_items.where(:url_type => url_type)
    url_items.each do |url_item|
      csv << self.process_row(url_item)  # simple array [1,"bob"]
    end 
  end 
  return csv_data
end
March 26, 2012 - (<= v3.1.0)
1 thank

Use exist scopes on default_scope - pay attention

To use exists scopes on default_scope , you can use something like:

class Article < ActiveRecord::Base
  scope :active, proc {
    where("expires_at IS NULL or expires_at > '#{Time.now}'")
  }

  scope :by_newest, order("created_at DESC")

  default_scope by_newest
end

But, if you would add a filter, and it require a lazy evaluate, use block on default_scope declaration, like:

default_scope { active.by_newest }
March 22, 2012 - (>= v3.1.0)
2 thanks

Deprecation in 3.1+

In Rails 3.1 and higher, just use ruby’s SecureRandom, e.g.

Before

ActiveSupport::SecureRandom.hex

After

SecureRandom.hex
March 18, 2012
0 thanks

Use Join to Turn Array Items into a String.

If you’re looking to take an array like

[ 'don', 'draper' ]

And get

'don draper'

Then use join instead:

[ 'don', 'draper' ].join( ' ' ) 

#=> 'don draper'
March 18, 2012
0 thanks

Destructive to the Original String.

Just as an FYI this function is destructive to the original String object.

name = 'draper' #=> "draper"

name.insert( 0, 'don ' ) #=> 'don draper'

name #=> 'don draper'
March 15, 2012 - (>= v3.2.1)
1 thank

Deprecated proxy_owner

Just change your

proxy_owner

calls to

@association.owner

Found it here: http://mileszs.com/deprecation-warnings-for-proxyowner-in-rails

March 10, 2012
3 thanks

flash messages

In rails 3.1 the following does not work for me

redirect_to { :action=>'atom' }, :alert => "Something serious happened" 

Instead, you need to use the following syntax (wrap with parens)

redirect_to({ :action=>'atom' }, :alert => "Something serious happened")
March 5, 2012
0 thanks

Example of conditions using

f.e.

validates :number, :presence => { :if => :quota_file? }

def self.quota_file?
  quota_file?
end
March 5, 2012
0 thanks

won't refresh updated_at

This will not cause :updated_at column to refresh, while ActiveRecord::Base#increment! would.

February 23, 2012
0 thanks

Be careful with path vs. endpoint

URI.join uses a delimiter – forward slash (/) – to decide if joined strings are a path or endpoint. In order to include strings as part of the path, they must end with a forward slash (/). Otherwise, they are assumed to be an endpoint and are overritten by your new “endpoint”.

Used this way, it (kind of) makes sense:

1.9.2p290 :021 > URI.join("http://localhost/test","main.json")
 => #<URI::HTTP:0x007fa68e81c270 URL:http://localhost/main.json> 

1.9.2p290 :022 > URI.join("http://localhost/test/","main.json")
 => #<URI::HTTP:0x007fa68e80e0d0 URL:http://localhost/test/main.json> 

It is especially confusing when you pass 3 strings and the 3rd (your endpoint) overwrites the 2nd (which you expected to be part of the path).

1.9.2p290 :023 > URI.join("http://localhost/", "test", "main.json")
 => #<URI::HTTP:0x007fa68cec0ba0 URL:http://localhost/main.json> 

1.9.2p290 :024 > URI.join("http://localhost/", "test/", "main.json")
 => #<URI::HTTP:0x007fa68ce14c60 URL:http://localhost/test/main.json> 

Now, consider that you are probably using a variable for the string value of ‘test’.

1.9.2p290 :025 > controller = 'test'
1.9.2p290 :026 > URI.join("http://localhost/", controller, "main.json")
 => #<URI::HTTP:0x007fa68cec0ba0 URL:http://localhost/main.json> 

Your `controller` is simply ignored. Or rather, your endpoint(?) was overwritten.

I’m not sure what versions of ruby this affects. As you can see I am using 1.9.2p290.

February 20, 2012
0 thanks

How to user method 'from'

example

User.from('posts').to_sql
# => "SELECT `users`.* FROM posts"
February 20, 2012
0 thanks

How to user method from'

example

User.from('posts').to_sql
# => "SELECT `users`.* FROM posts"