Thursday 24 November 2016

How to count Active records with condition ? - Advanced SQL query

Let's take an example. If you would like to fetch list of students with how many books they purchased, and suppose your transaction table contains book status as "Requested" and "Purchased".

Following query will help you to achieve this :

@students = Student.joins(:student_books).where("ANY CONDITION").select("*, count(CASE WHEN student_books.status = 'Purchased' THEN 1 END ) as purchased_book_count").group("student_books.student_id")
You can access this  purchased_book_count as following:

student = @students.first
purchased_book_count = student.purchased_book_count

Happy Coding ...!!

Thursday 27 October 2016

To copy file from remote server to local machine

Try following command to copy any file from remote server to your local machine .

scp -r -P port usrname@ip:/path-to-folder .

example: scp -r -P 27000 abc@10.70.12.12:/tmp/hotel_dump .
port = 27000 username = "abc" , remote server username path-to-folder = tmp/hotel_dump . = current local directory

Friday 7 October 2016

Difference between <%, <%=, <%# and -%> in ERB in Rails?

<% %>
Executes the ruby code within the brackets.
<%= %>
Prints something into erb file.
<% -%>
Avoids line break after expression.
<%# %>
Comments out code within brackets; not sent to client (as opposed to HTML comments).

Friday 30 September 2016

Trigger an event in input text after I stop typing/writing?

How to trigger an event in input text after I stop typing/writing?



You need to include:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="text" id="example" />//
// $('#element').donetyping(callback[, timeout=1000])
// Fires callback when a user has finished typing. This is determined by the time elapsed
// since the last keystroke and timeout parameter or the blur event--whichever comes first.
//   @callback: function to be called when even triggers
//   @timeout:  (default=1000) timeout, in ms, to to wait before triggering event if not
//              caused by blur.
// Requires jQuery 1.7+
//
;(function($){
    $.fn.extend({
        donetyping: function(callback,timeout){
            timeout = timeout || 1e3; // 1 second default timeout
            var timeoutReference,
                doneTyping = function(el){
                    if (!timeoutReference) return;
                    timeoutReference = null;
                    callback.call(el);
                };
            return this.each(function(i,el){
                var $el = $(el);
                // Chrome Fix (Use keyup over keypress to detect backspace)
                // thank you @palerdot
                $el.is(':input') && $el.on('keyup keypress paste',function(e){
                    // This catches the backspace button in chrome, but also prevents
                    // the event from triggering too preemptively. Without this line,
                    // using tab/shift+tab will make the focused element fire the callback.
                    if (e.type=='keyup' && e.keyCode!=8) return;
                    
                    // Check if timeout has been set. If it has, "reset" the clock and
                    // start over again.
                    if (timeoutReference) clearTimeout(timeoutReference);
                    timeoutReference = setTimeout(function(){
                        // if we made it here, our timeout has elapsed. Fire the
                        // callback
                        doneTyping(el);
                    }, timeout);
                }).on('blur',function(){
                    // If we can, fire the event since we're leaving the field
                    doneTyping(el);
                });
            });
        }
    });
})(jQuery);

$('#example').donetyping(function(){
  $('#example-output').text('Event last fired @ ' + (new Date().toUTCString()));
});That will execute when:
  1. The timeout has elapsed, or
  2. The user switched fields (blur event)
(Whichever comes first)

Monday 26 September 2016

Start multiple servers for same project with different ports

Rails uses the pid of your running server to check whether a server is already running or not.
This is stored under tmp/pids/server.pid If you want to run the same application again , you need a different pid for your server.
rails s -p 4000 --pid tmp/pids/server2.pid
Happy Coding..!!

Friday 23 September 2016

How to hide border of overlapping div using css

Just use below mentioned code to hide overlapping border of two div. 


.class_name + .class_name {
border-top: none;
}

Wednesday 21 September 2016

Open mail box by clicking email id and calling functionality on click of mobile no

It will open mail popup if user is already login/ thunder mail implemented

<%= mail_to @contact_email %>

In view page to add calling feature for mobile number (for mobile view) - Use gem 'tel_to_helper'

<%=tel_to @contact_phone %>

Tuesday 20 September 2016

Ruby On Rails Database Backup and Restore


MySQL


You can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database.


Backup:

mysqldump -u user -p database_name > dump.sql
(Enter password)

Restore:

mysql -u user -p new_database_name < dump.sql
(Enter password)



Postgres Database backup and Restore to dump file

Here is the quick snippet of how backup and restore postgres database 

Backup:

1

"pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"

Restore:

1

"pg_restore --verbose --host #{host} --username #{user} --clean --no-owner --no-acl --dbname #{db} #{Rails.root}/db/#{app}.dump”


Postgres Database backup and Restore to sql file



Backup:

1

$ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}

Restore:

1

psql -U {user-name} -d {desintation_db}-f {dumpfilename.sql}