question?
Shouldn't the second example be:
[1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8], [nil,6,9]]
??? or am I missing something?
Community contributions, tips, and corrections to the documentation. (1708 notes)
Shouldn't the second example be:
[1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8], [nil,6,9]]
??? or am I missing something?
Is there any place where there is a full listing of RegEx syntax?
In Rails >= 2.3 you can't use formatted_xxx url helpers anymore.
However, you can still pass a :format option to url helpers, eg:
articles_path(:format => :csv) # => /articles.csv
For some reason the each_char method on String is not available by default in Ruby 1.8.6 and you will be presented with a NoMethodError.
You can resolve this by requiring the jcode lib:
require 'jcode'
Via Kenneth Kalmer:
From the man page: If salt is a character string starting with the characters “$id$” followed by a string terminated by “$”: $id$salt$encrypted then instead of using the DES machine, id identifies the encryption method used and this then determines how the rest of the password s...
Sometimes you need to use select_tag instead of select (because you're after more control or need to use optgroups, for example), but still want the id/name conventions that select would give.
In this case, all you need to do is set the first parameter to whatever would be produced by select, and...
How about if you wanted to find a random set of records instead of a singular record, what would be the best way?
Thank you
Here is my favorite idiom for creating a Hash from an Array of keys and an Array of values:
keys = [:a, :b]
values = [1,2]
h = Hash[*keys.zip(values).flatten] # => {:b=>2, :a=>1}
You can just use a set difference (aka minus) to see if one array includes all elements of another
not_included = [1,2,3] - (1..9).to_a
not_included # => []
not_included = [1,2,3,'A'] - (1..9).to_a
not_included # => ["A"]
Use intersection to test if any of the one are...
Ordering by RAND() is not a wise idea when you have a large table with lots of rows. Your database will have to calculate a different random value for every row in your database -- O(N) -- then sort the entire table by those values -- O(N log N).
There ar...
It's as simple as: Things.first(:order => 'RAND()')
Of course depending on your database it could be 'RANDOM()' or something similar.
(From Obie Fernandez/ The Rails Way, ISBN 978-0321445612. Thanks Obie!)
This caveat:
The value of the counter cache column must be set to zero by default in the database! Otherwise the counter caching won’t work at all. It’s because the way that Rails implements the counter caching behavior...
When working with the contents of a directory it's not uncommon to be interested in a specific subset of the entries present.
Dir.glob can be used to fetch entries by name and File.stat can be used to determine the type of file.
There is a small gotcha - this caught me up for a while.
If you are using implicit multipart mime types by naming your template xxx.text.html.erb and xxx.text.plain.erb, you will need to change your template name back to the original xxx.erb.
If you use the implicit template name, your attachmen...
If you want to get up to speed with Rails' caching and haven't seen it already, definitely check out this video series on Scaling Rails:
Use :methods parameter to include ActiveRecord instance methods to JSON output. :only and :except uses DB columns only.
@events.to_json(:include => {
:images => {
:only => [], :methods => [:public_url] }})
In the previous example events have multiple...
This isn't gone, it's just been moved to the ActiveSupport module namespace.
See: ActiveSupport::Inflector#pluralize
Often when defining a to_param method, it's handy to introduce an opposite method for decoding them. For example:
class User < ActiveRecord::Base
def self.from_param(param)
find_by_name!(param)
end
def to_param
name
end
end
While you can just as easily redefine...
Ryan Daigle has a great article about 2.3's new nest forms which does a really good job of explaining how to use this and some of the potential gotchas. Highly recommended:
http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
code example:
class Company < ActiveRecord::Base
has_many :route_lists
end
class RouteList < ActiveRecord::Base
belongs_to :company
has_many :routes
end
class Route < ActiveRecord::Base
belongs_to :route_list
has_one :company :through => :route_list
end...