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}" }
Community contributions, tips, and corrections to the documentation. (1708 notes)
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}" }
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 ig...
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
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) => "-"
=...
For Ruby 1.9 and later, use Kernel#Array to get this functionality.
See class IO for file open modes
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...
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')
You'd be much better off using the Timecop gem ( https://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).
Be careful, conditional rescue_from does not work in Rails 3.2.11
This method keeps track of whether a hidden id field needs to be created when the form builder is generating fields for a nested model. It gets set to true by #hidden_field when that method is used to create a field named 'id'.
Here's an example of what this can be useful for: http://railsforum.com...
see: http://github.com/rails/rails/pull/6985
class Article < ActiveRecord::Base scope :locked, where(... end
will cause intermittent problems of the type
undefined method 'locked' for #<Class:0x007fdab3025298>
Use something li...
==== Here is the working one each_with__index:
a=[11,22,31,224,44].each_with_index { |val,index| puts "index: #{index} for #{val}" if val < 30}
index: 0 for 11
index: 1 for 22
=> [11, 22, 31, 224, 44]
Below couldn't produce the output, as with_index couldn't work on the...
I found a very good post on +SO+ - which clearly explained the difference between enum#with_object and enum#each_with_object. The link is as follows:
Enumerator#with_index has confusing documentation, but hopefully this will make it clearer.
==== Code example
a=[11,22,31,224,44].to_enum
=> [11, 22, 31, 224, 44]
a.with_index { |val,index| puts "index: #{index} for #{val}" }
index: 0 for 11
index: 1 for 22
index: 2 for 31...
##
class Exam cattr_reader :code, :description, :points, :instance_reader => false
@@code = "EXM"
@@description = "Sent Exam"
@@points = 1000
end
=== In this case it's possible to use
Exam.code # => EXM Exam.description # => Sent Exam Exam.points # => 1000
What works for Active Record, also works for Mongoid:
de:
mongoid:
attributes:
picture:
explanation: Description
Note that typically if you want to connect to an SQLite database the adapter would be "sqlite3"; not "sqlite" as depicted in the documentation above. Just using the term "sqlite" might result in the error message: "database configuration specifies nonexistent sqlite adapter"
An +:onchange+ call can be made using the Rails 3 +jquery-ujs+ helper in the form: check_box_tag( name, value, checked, html_and_other_options) For example: select_tag( "my_tag_id", entity.id, class: "progress bar update_msg", disabled: disabled? data: { remote: true, url...