Wednesday 20 February 2013

Querys in Ruby on Rails

Query's in Ruby 

  In this post we discuss about how to retrieve  data from database like mysql or sql ...

     Select
 
        Model.where("column = ? , value)
        Model.where("column = ? or other_column = ?", value, other_value)
        Model.where("column = ? and other_column = ?", value, other_value)

Sunday 17 February 2013

Rails Time/Date Formats (strftime)


Rails Time/Date Formats (strftime)

Method:

               <%= @contact.created_at.strftime("%d/%m/%Y")  %>


 Year
%Y     year with century 2007
%y     year without century 07
%C     century number (year divided by 100) 20
Month
%B     full month name January
%b     abbreviated month name Jan
%h     same as %b Jan
%m     month as number (01-12)
Week
%U     week number of the year, Sunday as first day of week (00-53)
%W     week number of the year, Monday as first day of week (00-53)
Day
%A     full weekday name Wednesday
%a     abbreviated weekday name Wed
%d     day of the month (01-31)
%e     day of the month, single digits preceded by space ( 1-31)
%j     day of the year (001-366)
%w     weekday as a number, with 0 representing Sunday (0-6)
%u     weekday as a number, with 1 representing Monday (1-7)
Time
%H    hour (24-hour clock) (00-23)
%k     hour (24-hour clock); single digits preceded by space ( 0-23)
%I     hour (12-hour clock) (01-12)
%l     hour (12-hour clock); single digits preceded by space ( 1-12)
%M     minute (00-59)
%S     seconds (00-59)
%p     either AM or PM AM
%Z     timezone name or abbreviation EDT
%z     timezone offset from UTC -0400



Thursday 14 February 2013

Ruby installation and create application

Ruby on Rails

Installation:

  • Rails-3.2.11
  • Ruby 1.9.2

 Ruby Installation:

      •   curl -L https://get.rvm.io | bash -s stable
      • --bash -sk stable < <(curl -sk                  https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
      • [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
      • rvm install 1.9.2(if you need 1.9.3  you change 1.9.3)

Rail instalation:


                            gem install rails r=3.2.11

Create New Project:

           step 1 -->[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

           step 2 -->rvm --create use1.9.2@name(gemset give any name)

           step 3--> rails new blog(projectname)

   step 4--> rm -rf Gemfile.lock

          step 5->change gem 'mysql2' in GemFile
           step 6-->bundle install(used for maintain rail version)
-->Exception
An error occurred while installing mysql2 (0.3.11), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.11'` succeeds before bundling.
-->Solution
       sudo apt-get install libmysqlclient-dev

          step 7--> rake db:create
Exception:
                      Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
solution
-->gem install execjs

        step 8--> rails g scaffold Question(give your application name any name) title:string             
                      questions:string answers:string(field name)
                                                                       rails      mysql or other
:binary     blob
         :boolean    tinyint(1)
:date     date
:datetime   datetime
:decimal    decimal
:float   float
:integer    int(11)
:string     varchar(255)
:text     text
:time     time
:timestamp  datetime


step 9-->-->rake db:migrate

step10-->rails server

       step 11:open browser type url localhost:3000/ blog (projectname)

   if  you add model or controller to your application follow below instruction 

      model
                 rails g model User(modelname)
      controller
                 rails g controller User (controller name)
      mailer
                rails g mailer  user_mail(mailer name)
      migration 
             scaffold
               rails g scaffold Question(give your application name any name) title:string          
                      questions:string answers:string(field name)
       add new-column
                    rails g migration add_fieldname_to_tablename  fieldname:string