Recent notes
RSS feed:include is also valid option
my_company.serializable_hash(:include => [:people])
Bug in Ruby or this documentation
%Q doesn’t return microseconds but milliseconds! Use %s%6N for microseconds.
Bug in Ruby or this documentation
%Q doesn’t return microseconds but milliseconds! Use %s%6N for microseconds.
use validates :name, :presence => true instead
validates_presence_of is a holdover from the Rails 2 days.
This is the way it is done now http://guides.rubyonrails.org/active_record_validations_callbacks.html#presence
@UnfalseIdeas
What is the purpose of
Hash[one: 1, two: 1]
When you can write
{one: 1, two: 2}
Aren’t you just passing a hash into the [] method?
be aware that this writes to tmp/cache
Its supposed to be http caching, but Rails will actually cache the response to whatever you specified as the cache store, *as well*, but only if you specify :public => true. The default is filestore so it will try to write to tmp/cache.
Only a problem if you don’t have the proper permissions set, in that scenario your apache/nginx logs could fill up very quickly with “permission denied errors”
Full explanation is here http://blog.tonycode.com/archives/418
increment_by_sql for PG
Note, if you’re using the code below for incrementing by SQL with a Postgres database, it’s not going to like the backticks. Just remove them:
def increment_with_sql!(attribute, by = 1) raise ArgumentError("Invalid attribute: #{attribute}") unless attribute_names.include?(attribute.to_s) original_value_sql = "CASE WHEN #{attribute} IS NULL THEN 0 ELSE #{attribute} END" self.class.update_all("#{attribute} = #{original_value_sql} + #{by.to_i}", "id = #{id}") reload end
HTTPS request
Hey, guys!
You have one mistake in example code.
uri = URI('https://secure.example.com/some_path?query=string') Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https').start do |http| request = Net::HTTP::Get.new uri.request_uri response = http.request request end
Here HTTP::start method called twice. This code should look like
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| request = Net::HTTP::Get.new uri.request_uri response = http.request request end
It’s work - I checked.
Passing in an Array instead of individual arguments
Pass in array instead of list
h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } keys_i_want = %w(cow cat) h.values_at(*keys_i_want) #=> ["bovine", "feline"]
Use concat insted of joining collection explicitely
concat method will be useful to join the collection object from looping conditions.
arr = ["a", "b", "c"] content_tag(:ul, :class => 'a class') do arr.each do |item| concat content_tag(:li, item) end
And this will generate the html as shown below
<ul class="a class"> <li>a</li> <li>b</li> <li>c</li> </ul>
output GBK
‘I am 中国人’.encode(‘gbk’,‘utf-8’)
When dealing with has_many through
The non-repeating primary key id must be used with find_in_batches.
-
User has many things
-
User has many socks through things
-
Sock has many things
-
Sock has many users through things
For the sake of argument, assume the first user has two socks and all other users have one sock. There are 1000 users in total and 1001 socks in total.
User.joins(:socks).count => 1001 agg = [] # Incorrect User.joins(:socks).find_in_batches{|g| agg += g} agg.count => 1000 Sock.joins(:users).count => 1001 agg = [] # Correct Sock.joins(:users).find_in_batches{|g| agg += g} agg.count => 1001
Out of date for Rails 3.
This is out of date for Rails 3; instead see ActiveRecord::Persistence#update_attributes
Beware - May cause performance issues
A serialized attribute will always be updated during save, even if it was not changed. (A rails 3 commit explains why: http://github.com/rails/rails/issues/8328#issuecomment-10756812)
Guard save calls with a changed? check to prevent issues.
class Product < ActiveRecord::Base serialize :product_data end
bad
product = Product.first product.save
good
product = Product.first product.save if product.changed?
Another Hash#without
Mange made me think, and I wanted to expand on his example with a small change.
class Hash def without(*keys) dup.without!(*keys) end def without!(*keys) reject! { |key| keys.include?(key) } end end h = { :a => 1, :b => 2, :c => 3 } h.without(:a) #=> { :b => 2, :c => 3 } h #=> { :a => 1, :b => 2, :c => 3 } h.without(:a, :c) #=> { :b => 2 } h.without!(:a, :c) # { :b => 2 } h #=> { :b => 2 }
Minor correction to Rubybull's examples?
Was your first example intended to be:
a=[11,22,31,224,44] => [11, 22, 31, 224, 44] a.each.with_index { |val,index| puts "index: #{index} for #{val}" }
Exceptions raised within are ignored.
From http://guides.rubyonrails.org/active_record_validations_callbacks.html
The after_commit and after_rollback callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don’t interfere with the other callbacks. As such, if your callback code could raise an exception, you’ll need to rescue it and handle it appropriately within the callback.
Default field order
If you want to set an app wide default order for the fields (rather than passing :order each time), use the locale file.
eg. edit config/locale/en.yml to include:
en: date: order: - :day - :month - :year
Using the undef/replace param overwrites the fallback parameter
If you want to provide a fallback Hash / Proc / Object you must not define the :undef and/or replace params since they overwrite the fallback.
How fallback works
fallback = Hash.new { '?' } fallback["\u2014"] = "-" "\u2014".encode!("ISO-8859-15", fallback: fallback) => "-"
Undef overwrites fallback:
fallback = Hash.new { '?' } fallback["\u2014"] = "-" "\u2014".encode!("ISO-8859-15", fallback: fallback, undef: :replace, replace: '?' ) => "?"
Where did this go?
For Ruby 1.9 and later, use Kernel#Array to get this functionality.
Checking content_for
There’s a much simpler way to check if content exists or not, and it’s been provided as example in docs since 05.2010:
module StorageHelper def stored_content content_for(:storage) || "Your storage is empty" end end
But this behavior was broken with SafeBuffer. You’ll be able to use it again when this issue (github.com/rails/rails/issues/9360) will be fixed and merged with master.
It also works with strings
It works with strings:
ActiveRecord::Base.connection.column_exists?('users', 'id')
Which is helpful if you need to specify the database/schema to use:
ActiveRecord::Base.connection.column_exists?('secondary.users', 'id')
Freezing Time.now
You’d be much better off using the Timecop gem ( rubygems.org/gems/timecop ) than than manually writing monkey-patches to freeze Time.now etc.
It also supports time travel (i.e. changing the time, but allowing the clock to continue running).
conditional rescue from does not seem working on Rails 3.2.11
Be careful, conditional rescue_from does not work in Rails 3.2.11