Posts

Simple way of importing and exporting excel, csv file for ruby on rails application using roo gem

Image
Create a Rails Application where student data can upload as excel and can be download as excel file. #Gemfile gem 'roo' #Student Table migration File class CreateStudents < ActiveRecord::Migration   def change     create_table :students do |t|         t.string :name         t.integer :roll_no         t.integer :marks1         t.integer :marks2         t.integer :marks3         t.integer :avg       t.timestamps null: false     end   end end #student_controller.rb class StudentController < ApplicationController   def s_home     @products = Student.all     respond_to do |format|       format.html       format.csv { send_data @products.to_csv }       format.xls { send_data @products.to_csv(col_sep: "\t"), filename: 'your...

All About Route in Rails

Image
What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. A web browser may be the client, and an application on a computer that hosts a web site may be the server. Get vs Post GET - Requests data from a specified resource. POST - Submits data to be processed to a specified resource. #Rails -- > config/route.rb root 'pages#index'   : default route (http://localhost:3000 it will go to this route) get 'authentication' => "login#authentication", (http://localhost:3000/authentication - it will goto login#authentication) get 'authentication' => "login#authentication", defaults: {format: 'json'}  now can represent data in json format.

Ruby on Rails Gems Install & Implementation

FIGARO: Purpose of Use. Figaro was written to make it easy to securely configure Rails applications. How to impliment . Step:1>>In Gemfile: gem 'figaro', '~> 1.1' Step:2>>$ bundle install or gem install figaro Step:3>>$ figaro install / rails generate figaro:install  It creates a commented config/application.yml file and adds it to your .gitignore.  Add your own configuration to this file and you're done! Step:4>> config/application.yml     DEV_DB_USERNAME: 'xx'   DEV_DB_PASSWORD: 'xxxxx'   DEV_DB_NAME: 'xxxxx' And here is my database.yml : development:   adapter: postgresql   encoding: unicode   database: <%= ENV['DEV_DB_NAME'] %>   pool: 5   username: <%= ENV['DEV_DB_USERNAME'] %>   password: <%= ENV['DEV_DB_PASSWORD'] %>   template: template0 AUDITED Purpose of Use. Audited (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited als...

How to get to get column name,datatype & length etc of a table.

Image
select column_name , data_type ,character_maximum_length from INFORMATION_SCHEMA.COLUMNS where table_name = 'donor_visit_details'; select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'donor_visit_details';

Important Query for PostgreSQL

Get All the Data of Table Select * from table_name; get the number of rows present table. Select count(*) from table_name; get dat of perticular column Select column_name from table_name; update data update table_name  set location_id = 10 # here whole location_id column will contain 10. if u want to update for specific row update table_name set mark = 100 where id = 1 Delete Data delete from table_name where id = 3  ---- one row delete from table_name where id in (3,6,7,8,9,11,12,13,16,17,18)  ---- multiple row alter table donor_visit_details_kgtemp  drop column weight --delete one column ALTER SEQUENCE table_name_id_seq RESTART with 107 # By this next ID will generate from 107 to onwards irrespecting of ID present

How to migrate Data for a Table from one DB to another DB through remote server? (Postgres , Mysql)

MySQL for whole DB  DB Backup:  mysqldump -u root -p [database_name] > dumpfilename.sql  DB  Restore:  mysql -u root -p [database_name] < dumpfilename.sql for a particular TABLE from a DB  Backup:  mysqldump -u root -p [database_name] [table_name] > dumpfilename.sql  Restore:  mysql -u root -p [database_name] < dumpfilename.sql PostgreSQL Here IP :192.168.1.36 Port :5433 User name : paras DB1 :kgp3_23apr15 DB2 :kgp6_23apr15 Create Back Up file for both Data Base . In Terminal. bishnu@bishnu:~$ /opt/PostgreSQL/9.0/bin/pg_dump -h 192.168.1.36 -p 5433 -U paras kgp3_23apr15 > kgp3_23apr15_24_bisnu.sql bishnu@bishnu:~$ /opt/PostgreSQL/9.0/bin/pg_dump -h 192.168.1.36 -p 5433 -U paras kgp6_23apr15 > kgp6_23apr15_24_bisnu.sql Back Up file name will generate like 'kgp3_23apr15_24_bisnu.sql' Back Up file name will generate like 'kgp6_23apr15_24_bisnu.sql' so that any time if we are doing any wrong migra...

Latest Rails Interview Question

What is callback in rails ? Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database. What is the difference between after create and after save ? Ans: after_create  only works once - just after the record is first created. after_save  works every time you save the object - even if you're just updating it many years later So if you want to do this email operation only just the once (and then never again) then use  after_create . If you want to do it  every  time the object is saved, then do it in  after_save after_create() Is called after Base.save on new objects that haven‘t been saved yet (no record exists). after_save() Is called after Base.save (regardless of whether it‘s a create or update save)