Tuesday, 5 July 2016

Rails 4, MySQL, and Emoji (Mysql2::Error: Incorrect string value: '\xF0\x9F\x8C\x9D')

Step 1
 

Change encoding from utf8 to utf8mb4
class ConvertTableToUtf8mb4 < ActiveRecord::Migration
  def change
    # For the table that will store unicode execute:
    execute "ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
    # For each column with unicode content execute:
    execute "ALTER TABLE `table_name` CHANGE column_name VARCHAR(226) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
  end
end

Step 2

 
Change database.yml according to the changes made.


development:
  adapter: mysql2
  database: database_name
  username: username
  password: password
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci


Thursday, 30 June 2016

Elasticsearch error – failed to connect to master




Elastic Search Version : 1.4.4
Device : Mac OS

When ever I start elastic search, sometimes I'll get an error like this

  • failed to connect to master [[Sea Urchin][OCeACmvgQsqw-uRUq25UFA][Srinivasan.local][inet[/192.168.1.12:9300]]], retrying...
 Eventually I found the root cause of this issue. As I’m running elasticsearch on a single server ( development environment ), I had to do only one thing

Go to config/elasticsearch.yml and enable

discovery.zen.ping.multicast.enabled: false

Monday, 18 May 2015

Rails 4 WITH NO Database



Sometimes, you may want to create a Rails application without ActiveRecord or a database. There might be situations like, Your application only store data using third party APIs, or storing in to files or might not have any persistent data at all. Since Rails stands for the common case (database backed application), by default it fails to start the server without a database connection.

You can simply build a Rails app without a database:
  1. Comment / Remove any database gems ( mysql, mysql2, sqlite, pg, mongoid, etc..) from your Gemfile and then run bundle. 
     
  2. Open your application.rb. By default, in Rails 4.0 you can see one line which requires all of Rails:
    require 'rails/all'
    This includes ActiveRecord also, and requires a database connection. Instead, you can include the specific parts of Rails that you gonna use.
    See below:
    require 'action_controller/railtie'
    require 'action_mailer/railtie'
    require 'active_model/railtie'
    require 'sprockets/railtie'
    require 'rails/test_unit/railtie'
     
  3. The default app generated by Rails 4.0 has one reference to active record which must be removed, in config/environments/development.rb:
    config.active_record.migration_error = :page_load 
    4. Remove any references to active_record in your configuration 
Now run your application with out any backend :)

Tuesday, 5 May 2015

Conference Track Management - Ruby solution

I got to solve this problem as the part of an interview process.
It wont look good if I paste the whole code here.

So just sharing the link to my gitbub account. Conference Track Management

Wednesday, 22 April 2015

Binary Search implementation in Ruby

  1. def binary_search(array, value, from=0, to=nil)
  2.     to = array.count - 1 unless to
  3.     mid = (from + to) / 2
  4.     if value < array[mid]
  5.       return binary_search(array, value, from, mid - 1)
  6.     elsif value > array[mid]
  7.       return binary_search(array, value, mid + 1, to)
  8.     else
  9.       return mid
  10.     end
  11. end

puts binary_search([1,2,3,4,5,7,8,9,10,12,15,16,17,18], 15)

Program to perform divison operation of two numbers without using /, %, and modules operator


def devide(divisor, dividend)
  quotient = 0
  until(dividend < divisor) do
    dividend -= divisor
    quotient += 1
  end
  quotient
end

puts devide(15, 100)

Wednesday, 1 April 2015

Ruby Count, Length & Size


These methods can be applied on Arrays, Hashes or Objects.  Here I'm just trying to illustrate the difference between count, length and size.

Array#length

Basically length method returns number of elements in the array.
Example, suppose we have array arr as,
arr = [1,2,3,4,5]
Calling Array#length method on array will give result as,
arr.length

 => 5 

Array#size

Array#size is just an alias to the Array#length method. This method executes same as that of Array#length method internally.
Example,
arr.size

 => 5 

Array#count

It has more functionalities than length or size. This method can be used for getting number of elements based on some condition. Unlike length/size, we can pass block or an argument to count.
This can be called in three ways:
  • Array#count => without condition, Returns number of elements in Array
  • Array#count(n) => passing value, Returns number of elements having value n in Array
  • Array#count{|i| i.zero?} => passing block, Returns count based on condition invoked on each element array

Example:
Way 1:
arr.count
=> 5
Way 2:
arr.count 3
=> 1
Way 3:
c.count{|n| n < 4}
=> 3