Arun Agrawal’s Blog

Ruby on Rails Developer

Serialized Attributes Rails 3

This post will guide you how to do Serialization for your attributes in Rails. Serialize means you want to save arbitrary Ruby data structure into the database. Let consider we have a User model in which we want to store preferences for user in a Ruby data structure format. Previously it was only YAML.
class User < ActiveRecord::Base
  serialize :preferences 
user = User.find 1
user.preferences = {:foo =>  'bar'}
So now we can pass a second parameter to serialize method to use that serialization method.
class User < ActiveRecord::Base
  serialize :preferences,
You need to implement this encoder! It can be a JSON, XML, Base64. Or what every encoding technique you like to use. A sample encoder look like this.
class Base64Encoder
  def load(value)
    return unless value

  def dump(text)
This new encoder must have these methods in it! So now you attribute is serialized and you can store data in it in your given format. Ok so now we talk about ActiveModel::Serialization ActiveModel::Serialization will give you serialized attribute for your classes. A very simple example to use ActiveModel::Serialization
class Post

  include ActiveModel::Serialization

  attr_accessor :title

  def attributes
    {'title' => title}


# So you can use like 

post =
post.serializable_hash   # => {"title"=>nil} = "Rails is Cool!!"
post.serializable_hash   # => {"name"=>"Rails is Cool!!"}

Can use two inbuilt Serialization techniques
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
This is a very short intro for Serialization. Hope i will write more in detail soon!! I got this from @tenderlove talk in RailsConf2011

Rails 3.0.10 and JRuby

Finally, The Rails is coming with template support with JRuby platform. We used to use “-m” option to generate new Rails Application for JRuby platform. Which is no more need now. Just set your platform to JRuby and create a new Rails Application as you do normally and it will generate a Application which is ready to go. No more tweaks required..!! This pull request which get merged into Rails 3-0-stable branch and it’s shipped with Rails 3.0.10.rc1 and will come with Rails 3.0.10. This feature is also coming with Rails 3.1. Showing here some of the example which will create Rails Application for JRuby platform. I have tested this on JRuby 1.6.3 version.
rvm jruby-1.6.3
# Will set your environment for JRuby 1.6.3 version.
Time to create rails app. This will generate a rails app for JRuby platform. Things like database.yml, Gemfile will generated specifically for JRuby platform.
rails new myapp
# SQLite version 3.x
#   gem 'activerecord-jdbcsqlite3-adapter'

  adapter: sqlite3
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  adapter: sqlite3
  database: db/test.sqlite3

  adapter: sqlite3
  database: db/production.sqlite3
source ''

gem 'rails', '3.0.10.rc1'

gem 'jruby-openssl'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://'

gem 'activerecord-jdbcsqlite3-adapter'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19', :require => 'ruby-debug'

# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
#   gem 'webrat'
# end
You can see in Gemfile the changes. It requires gem 'jruby-openssl' gem and gem 'activerecord-jdbcsqlite3-adapter' gem. Which is required for JRuby platform. Same work to generate for mysql and postgres.
rails new myapp -d mysql
And that’s all.!! Your Application is ready to run. Just do bundle install and all set. wOOtt..!! Post comments and discuss things if you guys still facing any issue. You can also post issue on Rails Issues and ask me to look into that. Some Useful links related to post:
  1. Activerecord-jdbc-adapter

Working as a Rails Contributor

Finally, I got the time to write something about my Rails Contributions. I have started contributing in April 2011. In start i usually do some test cases fixes and some updates.

After sometime i found that if you are using JRuby as a platform then you need to customize your template after generation. Or you need to use some custom template with -m option to customize your application.

I started digging into the Rails code base and i found that it can be added easily. Then… I just added and my pull request got accepted. wOOOttt!!

But that for master that means the feature will come in Rails 3.1 so after that i done some commits into 3-0-stable branch and yeah.. that feature is coming in Rails 3.0.10.

It’s really great to talking those guys on github. Specially when they ask you to change something which you have written wrong.

After that i started contributing in Rails more and more. It helps me to understand internal code and the basics of Ruby.

In recent we organized two bugmashs and we were the part of global Rails Hackfest. It was really very good. Enjoyed. Learnt a lot.

I am a part of BangaloreRubyUserGroup which helps a lot.





Rails3 Application With Jruby

If you are living on edge and you are using Rails3 then you need follow this. Rails3 With JRuby   Hi All, Recently i have started a Rails3 application which will use Jruby. I have gone through some of the steps for that application up and running. If you are using RVM then it’s easy to install Jruby. In the latest RVM version Jruby-1.6.0 is the default one. So my recommendation is first update the RVM itself then install Jruby. To update RVM and get the Jruby-1.6.0 installed
rvm update 
# But if you are already on the latest RVM then you will get message the "rvm update has been removed".

#To install Jruby

rvm install jruby
After installing Jruby you need to switch your environment to Jruby
rvm use jruby
#Using /Users/arunagw/.rvm/gems/jruby-1.6.0
Can check by using
~~>jruby -v                                                                                                                                                                          
jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
~~>ruby -v                                                                                                                                                                           
jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
Now you are ready to play with Jruby stuff. You can do some basic stuff like irb to test things
#IRB will also work
# JIRB will also work
Time to install Rails in Jruby environment
gem install rails 

#Will also work

jruby -S gem install rails
All set. Create your Rails application.
rails new my_app
Above command will create a rails application but not useful for Jruby platform. For all setting just run
rails new my_app -m
This will do all setup for you for a rails3 app in Jruby. You may need to change/update your Gemfile for your gems. Problems i faced. rake db:create will give you an error if you are using Mysql with Jruby.
uninitialized constant Mysql::Error
Here is the ticket information about this. Solution for this problem right now is to create database manually. After that all will work. No more hurdles i found. If you got in any problem let me know. We will try to solve that together. Links may be useful to find stuff 1. Jruby Activerecord adaptor. 2. My Sample Application 3. RVM —– Arun