:include is also valid option
my_company.serializable_hash(:include => [:people])
Community contributions, tips, and corrections to the documentation. (1708 notes)
my_company.serializable_hash(:include => [:people])
%Q doesn't return microseconds but milliseconds! Use %s%6N for microseconds.
%Q doesn't return microseconds but milliseconds! Use %s%6N for microseconds.
...
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
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?
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, i...
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)...
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 re...
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"]
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">...
'I am 中国人'.encode('gbk','utf-8')
require 'kconv'
then
“中国人民很行”.toutf8
.
Have look how #between? handle adge case. It's different from DateTime's.
Date.yesterday.between?(Date.yesterday, Date.tomorrow)
=> true
Date.tomorrow.between?(Date.yesterday, Date.tomorrow)
=> true
The non-repeating primary key id must be used with find_in_batches.
For the sake of argument, assume the first user has two socks and all other users have one sock. There are 10...
This is out of date for Rails 3; instead see ActiveRecord::Persistence#update_attributes
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 :pr...
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) #...