March 14, 2013
Mongoid: Constructing multiple attribute query criteria for embedded documents.

Let us say you have the following models:

class User
  embeds_many :book_marks, :validate => false, :cascade_callbacks => true  
end

class BookMark
  field :url, type: string
  field :category, type: string # personal, work
  field :tags, type: Array    

  embedded_in :user
end

Let us say we wount to count the number of users with book marks in personal category with technology tags.

User.where(:"book_marks.category" =>  "personal", 
  :"book_marks.tags".in => ["technology"]).count

For the data set below, the query will return both rows.

name          book_marks
              category  tags
============  ========  ====              
Tom           personal  politics
              work      technology  

Jack          personal  finance
              personal  technology  

We have to modify the query so that we multiple attributes of a single embedded document. MongoDB provides a construct called elem_match for such requirements.

User.where(:book_marks.elem_match => 
  { :category => "personal", :tags.in => ["technology"]).count

Now the query will only match Jack.

3:26pm  |   URL: http://tmblr.co/ZTlFMxgFeE4R
(View comments
Filed under: rails Mongoid 
March 8, 2013
Ruby assignment tricks (1.9.3)

Using an array to initialize multiple variables

>> a,b=[1,2]
=> [1, 2]
>> a
=> 1
>> b
=> 2

When the array is longer than the LHS variable count

>> a,b,c=[1,2,3,4]
=> [1, 2, 3, 4]
>> a
=> 1
>> b
=> 2
>> c
=> 3

When the array is shorter than the LHS variable count

>> a,b,c=[1,2]
=> [1, 2]
>> a
=> 1
>> b
=> 2
>> c # initialized to nil
=> nil

Using splat(*) operator at the LHS of the assignment

>> a,b,*c=[1,2,3,4]
=> [1, 2, 3, 4]
>> a
=> 1
>> b
=> 2
>> c # array containing rest of the elements
=> [3, 4]

Did you know ?

>> a = 1,2 # is same as a = [1,2]
=> [1, 2]

>> a,b=1,2 # same as a,b = [1,2]  OR a=1;b=2
=> [1, 2]
>> a
=> 1
>> b
=> 2

>> a = 1,b=2 # b is assigned to 2 but what about a?
=> [1, 2]
>> b
=> 2
>> a
=> [1, 2]

12:02am  |   URL: http://tmblr.co/ZTlFMxfmlC3w
(View comments
Filed under: ruby 
January 15, 2013
Fixing WEBrick::HTTPStatus::RequestURITooLarge exception in Rails

Add a file called webrick.rb file to the config\initializers directory.

if defined?(WEBrick::HTTPRequest)
  WEBrick::HTTPRequest.const_set("MAX_URI_LENGTH", 10240)
end

July 21, 2012
Array manipulation tricks

Given array:

[1, 1, 1, 2, 3, 4, 4, 4, 4, 5, 9, 9, 9]

Desired array:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

To get the desired array we need to

  • Declare the array

    a = [1, 1, 1, 2, 3, 4, 4, 4, 4, 5, 9, 9, 9]
    
  • Remove the two 1's at the beginning of the array.

    a[0..1] = [] # => [1, 2, 3, 4, 4, 4, 4, 5, 9, 9, 9]
    
  • Remove three 4's in the middle of the array.

    a[3..5] = [] # => [1, 2, 3, 4, 5, 9, 9, 9]
    
  • Insert 6, 7, 8 to the middle of the array.

    a[5..0] = [6,7,8] # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9]
    
  • Remove the two 9's at the end of the array.

    a[-2..-1] = [] # => [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

Interesting tricks:

  • Inserting using 0 terminated range

    a[5..0] = [6,7,8]
    

    When the range is terminated by a 0, the RHS value is inserted to the array at the index specified by the beginning of the range.

  • Removing items from the tail

    Following syntax will not work:

    a[-1..-2] = [] 
    

    This is because the range -1..-2 is an empty range(just like the range 2..1).

    (-1..-2).to_a # => []
    

    In a valid range, the starting number should be smaller than the ending number. The number -2 is smaller than the number -1. Hence the range is written as -2..-1.

    (-2..-1).to_a # => [-2, -1]
    

10:31pm  |   URL: http://tmblr.co/ZTlFMxPrAOTG
(View comments
Filed under: ruby array 
July 16, 2012
"Demanding you want something you clearly have no power to get is losing."

Josh Marshal . ( PS: I have modified the original quote by replacing the word Saying; at the beginning of the sentence; with the word Demanding.)

2:29pm  |   URL: http://tmblr.co/ZTlFMxPTszE2
(View comments
Filed under: quote 
July 11, 2012
Ruby implementation for Conway’s game of life

Here is my implementation for Conway’s game of life

June 28, 2012
Polymorphic and STI models working together in Mongoid

Here is an example of polymorphic and STI models working together:

class User
  include Mongoid::Document

  embeds_many :phones, :as => :identifiable
  embeds_many :emails, :as => :identifiable
  embeds_many :social_profiles, :as => :identifiable

  field :first_name, type: String
  field :middle_name, type: String
  field :last_name, type: String
  field :email, type: String
  field :password, type: String
end

class Identifier
  include Mongoid::Document
  embedded_in :identifiable, polymorphic: true  
  field :name, type: String # work, home, linked_in etc
  field :valid, type: Boolean
  field :validated_at, type: DateTime
end

class Email < Identifier
  field :email, type: String
end

class Phone < Identifier
  field :phone, type: String
end

class SocialProfile < Identifier
  field :url, type: String
end

June 20, 2012
Installing rmagick on Windows 7(64bit) + Ruby 1.9.3p194

Prerequisites:

Steps:

  • Download Win32 dynamic at 16 bits-per-pixel bundle. Do not use the 64 bit bundle.
  • Install the bundle to a directory without spaces (e.g., C:\ImageMagick). Select the option for the header files during installation.
  • Add ImageMagick exe directory to path(e.g., C:\imagemagick\ImageMagick-6.7.7-Q16). Read this article to find out how to set PATH.

  • Check ImageMagick installation: Launch a new command shell and check the location of the identify command.

    where identify
    
  • Install the rmagick gem ( notice the forward slash)

    gem install rmagick  -- --with-opt-dir=C:/imagemagick/ImageMagick-6.7.7-Q16
    

Reference

June 20, 2012
Segfault issues while running `thin` server on Windows + Ruby 1.9.3 + eventmachine-1.0.0.rc.1 

The issue is dicussed at length at github. Here is a temporary fix:

Clone the git repo

git clone https://github.com/eventmachine/eventmachine.git
cd eventmachine/ext

Edit extconf.rb and paste the following lines at line 103

found = RbConfig::CONFIG.values_at("CFLAGS", "CPPFLAGS").any? { |v| v.include?("FD_SETSIZE") }
$defs.push "-DFD_SETSIZE=32767" unless found

Install the gem using the cloned+modified repo

cd ..
gem build eventmachine.gemspec
gem install eventmachine-1.0.0.rc.1.gem

Run the thin server in project directory

thin start

June 18, 2012
Samsung smart TV - USB HDD folder refresh issue

I have a Samsung smart TV (UN55D8000) which supports USB hard drives. In the beginning, I was able to connect my Seagate FreeAgent 1 TB HDD to the TV and play any video files. But the TV stopped recognizing any new files/folders added to the disk. Even renaming the HDD didn’t make any difference.

Recently, I found a solution(in CNET forums) that seem to work.

In the file explorer, go to the media type selector on the top right hand corner. Now you are shown an option to select Video/Picture/Audio. Select Picture and wait until the control is returned back to you. Now select the media type selector menu again and choose Video. Now all your new files will be shown

Liked posts on Tumblr: More liked posts »