Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Wednesday, August 21, 2013

Rails 4 - Scope

In rails 4, scopes should take a proc object or a block. Take an example this User model
Class User < ActiveRecord::Base
  attr_accessor :name,:age,:status

  def initialize(name, age)
    @name = name
    @age = age
  end
end
I would like to make default scope based on status 'user' and admin scope based on status 'admin'. In rails 3, I can make scope like this
#Rails 3
scope :admin, where(status: 'admin')
default_scope where(status: 'user')

Rails 4, I can make scope this way
#Rails 4
default_scope { where(status: 'user') } #  block
#default_scope ->{ where(status: 'admin) } #Proc valid
scope :admin, ->{ where(status: 'admin') }
If we try to use scope without(see rails3 examples) passing proc or block in Rails 4, it triggers DEPRECATION WARNING message.

Rails 4 - Finders

Old-Style finders are deprecated
In Rails 4, Old-Style finders are deprecated. Consider User model with name, age members
Class User < ActiveRecord::Base
  attr_accessor :name,:age

  def initialize(name, age)
    @name = name
    @age = age
  end
end

In this class, If we want find user with name of 'foo'. See the difference in rails 3 and rails 4 call
#Rails 3
User.find(:all, conditions: { name: 'foo' })
#Rails 4
User.where(name: 'foo')

In rails 4, you will get DEPRECATION WARNING: Calling #find(:all) is deprecated. You can call #all directly instead or build a scope instead of using finder options.

Dynamic finders that return collections are deprecated
#Rails 3
User.find_all_by_name('foo')
#Rails 4
User.where(name: 'foo')

In Rails 4 alerts DEPRECATION WARNING if we use dynamic method(User.find_all_by_name). Preferable alternative option is User.where(...).all

Thursday, March 14, 2013

TextMate bundles/plugins - Ruby on Rails application development

As we know the best choice of editor to develop Ruby on Rails application on Mac is TextMate. As a developer we always think application quality and easiest way of development. As we know developing rails application is fun. I think some of the TextMate plugins help developing Ruby on Rails application even more fun.

Ruby on Rails - It's one of the nice bundle for rails application development. Learn it with PeepCode - http://peepcode.com/products/textmate-for-rails-2

PeepOpen - It's a file navigation application. It helps to search a file in your project with just typing few characters of the file name.

JavaScript Tools - It contains Essential Tools for developing Javascript in TextMate

jQuery - It's a jQuery bundle for TextMate


Monday, May 7, 2012

Install & setup Passenger module on Apache2 - Ubuntu 11.10

This guide help you to deploy Rails 3 application on apache2.

1. Install passenger gem on you're machine
$ sudo gem install passenger

2. Install apache2 module for passenger
$ sudo passenger-install-apache2-module

It does following three steps

a. The Apache 2 module will be installed for you.

If any software not available on you're server to install apache 2 module. It will guide you about missing software and some suggestion how to install those missed software

Once all support software available, then it compile & install apache 2 module.

b. Guide you how to configure apache

Once apache2 module installed, it guide you to configure apache - display LoadModule, RailsSpawnServer and RailsRuby location which will be required to configure passenger module on apache(httpd.conf).Just copy and paste those lines to httpd.conf

c. Guide you how to deploy a Ruby on Rails application
<VirtualHost *:80>

ServerName www.yourhost.com

DocumentRoot /somewhere/public

</VirtualHost>

add this virtualhost configuration on httpd.conf file

Now i hope you're apache2 server ready to deploy rails 3 application. it use passenger module for apache2.

Thursday, March 15, 2012

Good Ruby on Rails Resources

These reference resources are very helpful and make you good in development and understand of  Ruby on Rails.

Official Rails Doc

General reference for Rails
Rails API docs with usage notes

Release - Rails 3.2.2

Rails 3.2.2 has been released. it contains two major security fixes and lots bug fixes.

Some of the major changes are

* Log files are always flushed

* Failing tests will exit with nonzero status code

* Elimination of calls to deprecated methods

* Query cache instrumentation includes bindings in the payload

* Hidden checkbox values are not set if the value is nil

* Various Ruby 2.0 compatibility fixes

More information about source and change log are available here

If you want to try latest rails on you're app, follow install and setup instructions which is available on official rubyonrails.org.

Sunday, February 12, 2012

Rails 3.2.1 has been released


Rails 3.2.1 has been released, with some fixes and doc improvements. 


If you would like to know more information and changes, have a look CHANGELOGs gist.

Saturday, January 28, 2012

Rails 3.2.0 released


It includes Routing & Faster dev mode, explain queries, tagged logger, store.

There’s a v3.2.0 tag on Github and 3-2-stable branch as well. You can see all the glorious details of everything that was changed in our CHANGELOG compilation.

For documentation,  refer 3.2 release notes with upgrade instructions, both the API docs and the guides have been generated for 3.2 as well, and there’s a brand new 3.2-compatible version of Agile Web Development with Rails.

Saturday, January 14, 2012

ActionController::RoutingError (No route matches [GET] "/assets/scaffold.css"):

In rails 3 applications, if any application doesn't refer static resources(js,images,css) such as

ActionController::RoutingError (No route matches [GET] "/assets/scaffold.css")
ActionController::RoutingError (No route matches [GET] "/assets/defaults.js")
ActionController::RoutingError (No route matches [GET] "/assets/logo.png")

Fix for those issues would be

Stylesheets will be in app/assets/stylesheets
Images will be in app/assets/images
javascripts will be in app/assets/javascripts


Tuesday, January 3, 2012

Install and Setup Ruby, Rails 3 on Ubuntu 11.04

To install Rails 3, system needs supporting packages(including ruby,rake,libtool,etc).

1. Install those support packages 

sudo apt-get install ri ruby ruby1.8 ruby-dev rake libruby1.8 zlib1g-dev libssl-dev libreadline5-dev libncurses5-dev build-essential curl git-core git-gui gitk libxml2 libxml2-dev libxslt1-dev bison autoconf libtool

2. Installing rails by RVM(Ruby Version Manager)
For more information about installing RVM, Check it here

sudo rvm install 1.9.2

To make version 1.9.2 as default ruby version(if you have multiple version ruby)

sudo rvm use 1.9.2 --default

3. Install rails gem

sudo gem install rails

4. Install sqlite3

To install this you may need "libsqlite3-dev". so get this from Ubuntu package repo 

sudo apt-get install libsqlite3-dev

Now rails 3 is ready for developing application, In general rails come up with in-build WEBrick webserver. So you can create and deploy sample application on WEBrick.

Sunday, September 4, 2011

Ruby on Rails 3.1 Released

Rails community has been released 3.1.0 version. It has major changes on ActionPack, ActiveModel, ActiveRecord, ActiveResource, ActiveSupport and Railties.

For more information about changes, please see each of the CHANGELOG files checked in to the Rails repository on github.

For an even more detailed list of changes, please see the commit list between Rails 3.0.10 and 3.1.0.