Using find_index to return first match. Profit!

l3x Sep 27, 2013 1 thank

This example shows how to use find_index to return a result as soon as the first occurrence of what you are looking for is found.

==== Code example
class Widget < Struct.new(:name, :profit); end

class WidgetManager
def initialize(*widgets)
  @widgets = widgets
end
def...

Anothery way

taimoorchangaiz Sep 25, 2013

==== This Worked For Me

require File.expand_path('../app/models/extenstions/active_record_ext', File.dirname(FILE))

I did this in application.rb

Edge case

bmaltzan Sep 13, 2013 2 thanks

NilClass#try doesn't check for methods on itself: nil.blank? #=> true nil.try :blank? #=> nil

touch in rails

Sabyasachig Sep 11, 2013

touch is used to update the updated_at column you can create a model instance and update the updated_at value on it

Let say we have one user model

user = User.first

next you can write

user.touch

so it will run the query for you

UPDATE users SET updated_at = 'current_date_time' WHERE `user...

Using reject to remove key/value pairs from a hash

bobfirestone Sep 9, 2013 1 thank

==== Code example # Remove empty strings { a: 'first', b: '', c: 'third' }.reject { |k,v| v.empty? } #=> {:a=>"first", :c=>"third"}

# Remove nil
{a: 'first', b: nil, c: 'third'}.reject { |k,v| v.nil? } # => {:a=>"first", :c=>"third"}

# Remove nil & empty strings
{a: '', b:...

everything is ok

evilguc Aug 20, 2013 2 thanks

Olefine, I'm not sure here is a good place for such questions (better use stackoverflow for example), but answer for your question is that Rails provide slice (and many other methods) not only for Hash class but for HashWithIndifferentAccess too such as for any other superclass of Hash, so they use...

for finding content

rakshit Aug 17, 2013

it will use all the field related to particular table so you can find data by any table field like Table name => ABC(:id, :name, :address) if you want to find data related to id or name or address than only write

ABC.find_by_id(1)

ABC.find_by_name("abc")

ABC.find_by_address("abc")

find_by_field...

expires_in option

concept47 Aug 15, 2013

You can actually pass in an expires_in option that sets how long Rails should show the fragment before deleting it so as an example ...

<% cache('homepage_sidebar', :expires_in => 10.minutes) do %>
<div>
  ...
</div>
<% end %>

This only used to work with memcached but it now w...