Skip to main content

Posts

Showing posts from April, 2014

MYSQL DATAT TYPES EXPLANATION?

MYSQL DATAT TYPES EXPLANATION?
What is data type

1. A data type specifies a particular type of data, such as integer, floating-point, Boolean etc.

2. A data type also specifies the possible values for that type, the operations that can be performed on that type and the way the values of that type are stored.
MySQL data types

MySQL supports a number of SQL standard data types in various categories. MySQL has Numeric Types, the DATETIME, DATE, and TIMESTAMP Types and String Types. Data types are discussed on this page are based on MySQL community server 5.6
MySQL Numeric Types

MySQL supports all standard SQL numeric data types which include INTEGER, SMALLINT, DECIMAL, and NUMERIC. It also supports the approximate numeric data types (FLOAT, REAL, and DOUBLE PRECISION). The keyword INT is a synonym for INTEGER, and the keywords DEC and FIXED are synonyms for DECIMAL. DOUBLE is a synonym for DOUBLE PRECISION (a nonstandard extension). REAL is a synonym for DOUBLE PRECISION (a nonstandard variati…

Paperclip uploading files (or)images to amazon s3 for Ruby on rails?

Paperclip uploading files (or)images to amazon s3 for Ruby on rails?

1)Add a gem
'gem 'aws-sdk'

2)bundle install

3)app/models/profile.rb-----Model add this code
has_attached_file :image,
                   :storage => :s3,
                   :bucket => "yyyyyy",
                   :s3_permissions => "public-read",
                   :path => "profiles/:id/:basename.:extension",
                   :s3_credentials => {
                   :access_key_id => "xxxxxxxxxxxxx",
                   :secret_access_key => "xxxxxxxxxxx"

    }
validates_attachment :file,:content_type => { :content_type => ["image/jpg", "image/gif", "image/png","application/pdf"] }

4)add coe for controller
def profile_image_create
 @profile = Profile.create(:file => params[:file])
end

===========Amazon s3 create a bucket=========
http://aws.amazon.com/s3/
Go to home page => click on…

rake db:rollback in rails

rake db:rollback in rails?

General-> rake db:migrate ->This will migrate your database by running migrations that are not run yet
Running ->specific Migration     ->rake db:migrate VERSION=20130717110623 ->    This runs specific migration file from your db/migrate directory having version 20130717110623
Running up from migration->     rake db:migrate:up VERSION=20130717110623 ->    This runs up part from your migration
Running down from migration ->    rake db:migrate:down VERSION=20130717110623     ->This runs down part from your migration
Re-Running Specific Migration ->    rake db:migrate:redo VERSION=20130717110623 ->    This will first run down part and then up part of the migration having version 20130717110623
Re-Running all migrations     ->rake db:migrate:redo ->    This will re-run all the migrations
Rollback a migration     ->rake db:rollback->     This will rollback last run migration(i.e. Default STEP=1)
Rollback last n migrations -&g…

Can you get DB username, pw, database name in Rails?

Can you get DB username, pw, database name in Rails?


Rails.configuration.database_configuration[Rails.env]
=> {"encoding"=>"unicode", "username"=>"postgres", "adapter"=>"postgresql", "port"=>5432, "host"=>"localhost", "password"=>"postgres", "database"=>"mydb", "pool"=>5}
(or)
config   = Rails.configuration.database_configuration
host     = config[Rails.env]["host"]
database = config[Rails.env]["database"]
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]

(or)
con = Mysql.real_connect("host", "user", "pw", "current_db")
(or)
Rails.configuration.database_configuration["development"]["database"]

Create migration files in rails from existing mysql table

Create migration files in rails from existing mysql table?Start by referencing your existing MySQL database in database.ymlrun rake db:schema:dump to generate the schema.rb filePaste the create_table methods from your schema.rb into a new migration, and Voila! ##generate schema.rb
ruby script/generate migration Tabledata

migration file inside add the schema.rb
remove this line
1)ActiveRecord::Schema.define(:version => 0) do
end
2)rake db:migrate

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

Mysql Queries

Mysql Queries?

1)createing table in mysql
create table users(id int not null primary key,name char(25),address varchar(100),dob date);
(or)
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

2)inserting data to tables;
insert into  users(id,name,address,dob) values(1,"sanjeevreddy","kachiguda",STR_TO_DATE('1-01-2012', '%d-%m-%Y'))


3)Add coloumn to mysql table
alter table users add column last_name varchar(255) not null ,add column  email varchar(240);


4)deleting table
drop table profiless;
drop table table_name;

5)updating table data
 update users set name ="reddy" where id = 1;

6)total databases
show databases;

7)show table attributes
desc table_name;
desc users;

=====================================
Database A database consists of one or more tables. A table is identified by its name. A table is made up of columns and rows. Colu…

Rails4 routes

Rails4 routes?

When a Rails application boots then it reads the config/routes.rb file. In your routes you might have code like this

Rails4demo::Application.routes.draw do
  root 'users#index'
  resources :users
  get 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
  get '/logout' => 'sessions#destroy', :as => :logout
  get "/stories" => redirect("/photos")
end

In the above case there are five different routing statements. Rails needs to store all those routes in a manner such that later when url is '/photos/5' then it should be able to find the right route statement that should handle the request.

In this article we are going to take a peek at how Rails handles the whole routing business.
Normalization in action

In order to compare various routing statements first all the routing statements need to be normalized to a standard format so that one can easily compare one route st…

How to Deploy a Rails 4 App With Git and Capistrano Setup your local Rails app to deploy to your production server using Capistrano and Git so your deployment process is automated, fast, and your brain hurts less.

How to Deploy a Rails 4 App With Git and Capistrano
Setup your local Rails app to deploy to your production server using Capistrano and Git so your deployment process is automated, fast, and your brain hurts less.
?


I think the two key aspects of any deployment process are speed and consistency. Speed means you can iterate and fix bugs fast and keep your production code in sync with your development code. Consistency means you know it's going to do the same thing every time so you're not afraid to actually do it and stay up to date. Using a revision control system like Git along with automated deployment recipes with Capistrano satisfies these criteria easily and gives your Rails app a little more oomph.

Back to the Future on Rails

This article assumes you're using a setup like I outlined in my article on how to setup a production server. That is, a Unix server with SSH access using Phusion Passenger and Apache serving a Ruby on Rails app.
Knowing is half the battle

Back whe…

Generate ERD diagrams for rails3 or highrer versions

Generate ERD diagrams for rails3 or highrer versions?

1)install sudo aptitude install graphviz
2)Add gem "rails-erd" to your application's Gemfile
3)bundle install
4)bundle exec erd

Another Method
1)gem install railroad
2)railroad -C | neato -Tpng > controllers.png
Controller diagram in PNG format
3)railroad -M | neato -Tpng > models.png
Model diagram in PNG format
railroad -M | dot -Tsvg > models.svg
Model diagram in SVG format
4)railroad -h

Deploying Rails app using Nginx, Unicorn, Postgres and Capistrano to Digital Ocean

Deploying Rails app using Nginx, Unicorn, Postgres and Capistrano to Digital Ocean?


Create droplet of your liking (ubuntu 12.10 x32)

ssh to root in terminal with your server ip

ssh root<a href="/123">@123</a>.123.123.123

Add ssh fingerprint and enter password provided Change password

passwd

Create new user

adduser username

Set new users privileges

visudo

Find user privileges section

# User privilege specification
root  ALL=(ALL:ALL) ALL

Add your new user privileges under root & cntrl+x then y to save

username ALL=(ALL:ALL) ALL

Configure SSH

nano /etc/ssh/sshd_config

Find and change port to one that isn't default(22 is default: choose between 1025..65536)

Port 22 # change this to whatever port you wish to use
Protocol 2
PermitRootLogin no

Add to bottom of sshd_config file after changing port (cntrl+x then y to save)

UseDNS no
AllowUsers username

Reload ssh

reload ssh

Don't close root! Open new shell and ssh to vps with new username(remember the port, or you're locked out!)