Skip to main content

Posts

Rails generate Csv or xls files

def get_all_users     csv = ""     csv1="Id,Email"     csv2 = ""     csv << "#{csv1}\n"     csv << "#{csv2}"     @user = User.find(:all)     @user.each do |x|        csv2 << "#{x.id}"        csv2 << ",\"#{x.email}\""        csv2 << "\n"      end    csv << csv2    send_data csv, :type => 'text/xls; charset=utf-8; header=present', :disposition => "attachment;filename=xyz.xls" (or) send_data csv, :type => 'text/csv; charset=utf-8; header=present', :disposition => "attachment;filename=xyz.csv" end In Views <p><%=link_to "hello",:controller => "user" ,:action => "get_all_users"%></p>

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 nonstandar...

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 => {                 ...

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 t...

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.yml run rake db:schema:dump to generate the schema.rb file Paste 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"]