Recent notes

RSS feed
February 18, 2015
0 thanks

Not only for strings, but arrays and hashes too

exclude? is defined as !include?, meaning it is the exact opposite of include? . See the source.

This means that it works for Arrays and Hashes too, as well as for Strings.

It works for Arrays:

>> [nil].exclude?(nil)
=> false
>> [nil].include?(nil)
=> true
>> ["lala"].include?(nil)
=> false
>> ["lala"].exclude?(nil)
=> true

And for Hashes:

>> params = {}
=> {}
>> params[:db] = "lol"
=> "lol"
>> params.exclude?(:db)
=> false
>> params.include?(:db)
=> true
>> 
February 12, 2015
0 thanks

reorder

adding to stevo’s comment that reorder is also usefull when you have default scope in your model. eg: default_scope -> { order(created_at: :desc) }

February 12, 2015
0 thanks

Example is a Bug!

Why is the example showing the use of the #detect method and not #find? Boggles the mind!

February 11, 2015
0 thanks

Redirect to subdomain

If you’re looking to redirect to a subdomain you can do things like this:

redirect_to users_url(1, params: {a: :b}, subdomain: 'bob')
February 11, 2015
0 thanks

Why is this deprecated?

Anyone knows?

February 8, 2015
0 thanks

prepend_before_filter

If you need the method to be called at *the beginning* of the before_filter chain then you should use:

prepend_before_filter

February 3, 2015
0 thanks

Poor man's maybe

After creating a simple Maybe monad in Ruby, a colleaque noticed I could have just used try (I wasn’t aware try supports blocks). I think the method was even meant for such cases.

Why I mention this? Because it clarifies the whole ‘raises exception if method does not exist’ thing. It should not be crappy solution to exception handling, but allow for doing away with messy if statements. An example:

report = params[:query_type]
.try { |qt| build_query(qt) }
.try { |sql| run_query(sql) }
.try { |res| format_result(res) }

If any of the expressions params[], build_query, run_query etc. returns nil, the chain is halted and nil is returned. It still throws exceptions if a values is not nil and method does not exist, which is just like it should.

February 2, 2015
0 thanks

Be careful with cycles

This simplistic implementation (unlike Marshal.load(Marshal.dump(object)) doesn’t handle cycles in objects.

a = {}
b = {a: a}
a[:b] = b
a.deep_dup # SystemStackError: stack level too deep
January 28, 2015 - (v1_8_6_287 - v1_9_3_392)
1 thank

Feature

A default pretty printing method for general objects. It calls pretty_print_instance_variables to list instance variables.

If self has a customized (redefined) inspect method, the result of self.inspect is used but it obviously has no line break hints.

This module provides predefined pretty_print methods for some of the most commonly used built-in classes for convenience.

January 16, 2015
0 thanks

An article about token-based authentication

www.codeschool.com/blog/2014/02/03/token-based-authentication-rails/

January 14, 2015
0 thanks

Have submit_tag send value as a nested resource

To have the submit_tag send it’s value within a nested resource for strong params use the name paramter.

submit_tag("Send", name: 'article[submit]')
January 8, 2015 - (v4.1.8)
3 thanks

Text improvement

Where it says: without loading a bunch of records should say: without loading a bunch of columns/attributes Considering that record usually is a row.

January 5, 2015
0 thanks

SQL Injection?

Note that the version of leente and timdorr are probably vulnerable to SQL Injection (through attribute param).

Probably you want to look into with_lock instead of handcrafting SQL.

January 5, 2015
1 thank

arguments do not need to be an array

it’s a small point, but if you look at the source, the method is defined with the splat operator in the arguments:

def select (*fields)

this means that a list of arguments is automatically converted to an array. There is no typo in the description above.

It will also work to pass an array:

select([:field1, :field2])

although the select method interprets this as a single argument, and places it into an array (due to the splat operator), this is then passed to the _select(*fields) method, which immediately calls fields.flatten!

So either a list or an array may be passed, both will work.

January 2, 2015
0 thanks

How safe is this?

Could this be used against a user supplied fragment like in a url route ?

December 29, 2014 - (<= v3.0.9)
0 thanks
December 16, 2014
2 thanks

Group method chain

The group_method parameter can be a string representing a method chain:

grouped_collection_select(:city, :country_id, @continents, 'countries.sort.reverse', :name, :id, :name)

If we were to modify the Country model so we can sort by name:

class Country
  include Comparable

  def <=>(other)
    self.name <=> other.name
  end
end

The above example would have given us the countries sorted by name in descending sequence.

December 12, 2014
0 thanks
December 9, 2014
0 thanks

See also ConditionVariable

If you need to and processing with respect to a particular resource between 2 or more threads in more complicated ways, it is likely that ConditionVariable is what you’re looking for.

November 27, 2014
1 thank

Adding index with other operator classes (PostgreSQL)

To perform on search by LIKE:

SQL Query:

SELECT users.* FROM users WHERE name LIKE 'Doug%';

Explain:

# Without index
Seq Scan on users  (cost=0.00..82183.32 rows=98524 width=418)
  Filter: ((name)::text ~~ 'Doug%'::text)

Adding index with operator class ‘varchar_pattern_ops’

add_index :users, :name, order: {name: :varchar_pattern_ops}
execute 'ANALYZE users;'

New Explain:

# With index
Bitmap Heap Scan on users  (cost=2444.46..56020.97 rows=98524 width=418)
  Filter: ((name)::text ~~ 'Doug%'::text)
  ->  Bitmap Index Scan on index_users_on_name  (cost=0.00..2419.83 rows=75940 width=0)
        Index Cond: ((name)::text ~>=~ 'Doug'::text)
November 24, 2014
1 thank

Arguments for .select must be array

Model.select(:field, :other_field, :and_one_more) has a typo. It must take an array of arguments as the description states:

Model.select([:field, :other_field, :and_one_more])

November 19, 2014
0 thanks

Also useful without respond_with

Using the class method #respond_to allows controller-level specification of the allowed mime-types. Without #respond_with , it enables a

Completed 406 Not Acceptable

response rather than

ActionView::MissingTemplate

error when an unsupported type is requested.

See: http://www.justinweiss.com/blog/2014/11/03/respond-to-without-all-the-pain/

November 19, 2014
1 thank

Bangladeshi Taka (BDT 1,200.95)

Code example

def to_bdt(amount)
  number_to_currency(amount, :unit => "BDT ", :separator => ".", :delimiter => ",")
end
November 17, 2014
0 thanks

Elements need to be in same order

Note that even if the arrays have the same content, the elements need to be ordered:

Example:

x = [1, 2, 3]
y = [3, 2, 1]
z = [1, 2, 3]

x.eql?(y) #=> false
x.eql?(z) #=> true
x.eql?(y.sort) #=> true
November 17, 2014 - (>= v3.1.0)
0 thanks

Include items affected in output

If the result returned from the block is an Integer, the output will include a message about that number of “rows” in addition to the elapsed time.

say_with_time "Some complex, custom work" do
  counter = 0
  # ... do some stuff here that increments the counter ...
  counter
end

#=> "-- Some complex, custom work"
#=> "   -> 45.3725s"
#=> "   -> 52880 rows"
November 17, 2014 - (<= v4.0.2)
0 thanks

Include items affected in output

If the result returned from the block is an Integer, the output will include a message about that number of “rows” in addition to the elapsed time.

say_with_time "Some complex, custom work" do
  counter = 0
  # ... do some stuff here that increments the counter ...
  counter
end

#=> "-- Some complex, custom work"
#=> "   -> 45.3725s"
#=> "   -> 52880 rows"
November 14, 2014
3 thanks

Opposite of persisted?

So I can find it when I look next time.

November 14, 2014
1 thank

Opposite of #new_record?

So that next time I look I find it.

November 7, 2014 - (v1.0.0 - v4.0.2)
0 thanks

Non Layout Pages

A 404 error for the favicon will be thrown on pages where there is no layout if there isn’t a favicon in the public folder.

A situation would be when a controller method is used to render an image and the user chooses to open the image in a new tab bypassing the layout and the favicon_link_tag.

November 6, 2014
8 thanks

Find the First Instance in the Table. If None Exists, Create One.

Specify the data you’re looking for. If it exists in the table, the first instance will be returned. If not, then create is called.

If a block is provided, that block will be executed only if a new instance is being created. The block is NOT executed on an existing record.

Code example

MyStat.where(name: statistic_name).first_or_create do |statistic|
  statistic.value = calculate_percentage
  statistic.statistic_type = "percentage"
end