Recent notes
RSS feedMakes 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.
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
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 }
Deprecation in 3.1+
In Rails 3.1 and higher, just use ruby’s SecureRandom, e.g.
Before
ActiveSupport::SecureRandom.hex
After
SecureRandom.hex
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'
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'
Deprecated proxy_owner
Just change your
proxy_owner
calls to
@association.owner
Found it here: http://mileszs.com/deprecation-warnings-for-proxyowner-in-rails
Add html5 scriipt async attribute
http://www.w3schools.com/html5/att_script_async.asp
javascript_include_tag "application", :async => true
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")
Example of conditions using
f.e.
validates :number, :presence => { :if => :quota_file? } def self.quota_file? quota_file? end
won't refresh updated_at
This will not cause :updated_at column to refresh, while ActiveRecord::Base#increment! would.
looking to bypass validations, callbacks and updated_at?
Take a look at updated_column
http://apidock.com/rails/ActiveRecord/Persistence/update_column
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.