Some times we wanted to know what are the previous values for an object.
We can track the unsaved attribute changes in Rails.
These can be used before save only.
The below are the methods you can use.
* changed
* changed?
* changes
Eg:
Before Assigning:
user = User.find(1)
user.first_name # => "Naga"
user.last_name # => "Harish"
user.changed? # => false
user.changed # => []
After Assigning:
user.first_name = "Turvi"
user.last_name = "Sri"
user.changed? # => true
user.changes # => {"first_name"=>["Naga", "Turvi"], "last_name"=>["Harish", "Sri"]}
user.changed # => ["first_name", "last_name"]
You can also you the dirty suffixes.
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']
user.first_name_changed? # => true
user.first_name_change # => ["Naga", "Turvi"]
user.first_name_was # => "Naga"
user.first_name_will_change! # => "Turvi"
NOTE: Once you save this record changed? will become false.
So you can have History as one more table or one more column called update_history as column to store all the changes.
Showing posts with label Rails. Show all posts
Showing posts with label Rails. Show all posts
Wednesday, May 26, 2010
Use of dirty objects
Labels:
change?,
changes,
dirty,
dirty object,
Naga Harish,
Rails,
Rails 2.2.2,
ROR,
Ruby
Tuesday, June 30, 2009
BitlaSoft 2nd Year Anniversary
This year too we had a great celebrations in our office for Bitlasoft's 2nd year Anniversary. This year we had a dark blue Jacket with a logo on the front side and punch line on the back side.
We had party with our family too. Nice enjoyment with all the quiz, Tambola, some silly questions, silly games :) .
We discussed all the cool things about the company. We wish all the best to all of us for a bright future of bitlasoft.
We had party with our family too. Nice enjoyment with all the quiz, Tambola, some silly questions, silly games :) .
We discussed all the cool things about the company. We wish all the best to all of us for a bright future of bitlasoft.
Labels:
Bitla Software,
Bitlasoft,
mobee.in,
Naga Harish,
Rails,
Ruby,
ticketSimply,
travel management
Friday, May 22, 2009
REST Client - Simply Test REST APIs
Our company prepared a new tool for testing the REST API's.
Using this you can Test the REST APIs.
GET, POST, PUT, HEAD, DELETE methods are supported.

Its very easy. Give the URL and select the method ex. "GET" and want to pass any parameters, you can also pass them.
Thats it!!
You will get the response.
Visit this link for a test. http://www.bitlasoft.com/tools/rest_client/
If you have any doubts you can mail to info@bitlasoft.com
Using this you can Test the REST APIs.
GET, POST, PUT, HEAD, DELETE methods are supported.

Its very easy. Give the URL and select the method ex. "GET" and want to pass any parameters, you can also pass them.
Thats it!!
You will get the response.
Visit this link for a test. http://www.bitlasoft.com/tools/rest_client/
If you have any doubts you can mail to info@bitlasoft.com
Labels:
Bitla Software,
Bitlasoft,
Naga Harish,
Rails,
REST,
REST Client
Saturday, April 25, 2009
Clone in Ruby
Clone is help ful for the values changed in the object but not the complete object change.
Let me explain with an example.
take an array
arr1 = ["a", "b", "c", "d"]
let me initiliza arr1 to arr2
arr2 = arr1
puts arr2
=> ["a", "b", "c", "d"]
If I change arr1 values i.e.,
arr1[1] = "z"
puts arr1
=> ["a", "z", "c", "d"]
then see arr2
puts arr2
=> ["a", "z", "c", "d"]
#This wil get changed because the whole arr1 is pointed to one block of memory and the same arr2 is also pointing to the same block. You can check this.
arr1.id and arr2.id which will be same.
puts arr1.id
=> 40972250
puts arr2.id
=> 40972250
Suppose if you want arr2 as the previuos arr1 and arr1 should get changed?
Do this:
arr1 = ["a", "b", "c", "d"]
arr2 = arr1.clone
puts arr2
=> ["a", "b", "c", "d"]
arr1[1] = "z"
puts arr1
=> ["a", "z", "c", "d"]
puts arr2
=> ["a", "b", "c", "d"]
But if you change the whole arr1 to some other values then arr2 will not change.
Eg:
arr1 = ["a", "b", "c"]
arr2 = arr1
puts arr2
=> ["a", "b", "c"]
arr1 = ["z", "y", "x"]
puts arr2
=> ["a", "b", "c"]
arr2 will be same as old arr1 because arr1 memory locaton will be different after the new initilaization.
You can check this by arr1.id and arr2.id
In this case clone is not needed.
More about clone (copied from Ruby book(Dave Thomas, The Pragmatic Programmers))---
clone obj.clone -> anObject
Produces a shallow copy of obj---the instance variables of obj are copied, but not the objects they reference. Copies the frozen and tainted state of obj. See also the discussion under Object#dup .
class Klass
attr_accessor :str
end
s1 = Klass.new » #
s1.str = "Hello" » "Hello"
s2 = s1.clone » #
s2.str[1,4] = "i" » "i"
s1.inspect » "#"
s2.inspect » "#"
Let me explain with an example.
take an array
arr1 = ["a", "b", "c", "d"]
let me initiliza arr1 to arr2
arr2 = arr1
puts arr2
=> ["a", "b", "c", "d"]
If I change arr1 values i.e.,
arr1[1] = "z"
puts arr1
=> ["a", "z", "c", "d"]
then see arr2
puts arr2
=> ["a", "z", "c", "d"]
#This wil get changed because the whole arr1 is pointed to one block of memory and the same arr2 is also pointing to the same block. You can check this.
arr1.id and arr2.id which will be same.
puts arr1.id
=> 40972250
puts arr2.id
=> 40972250
Suppose if you want arr2 as the previuos arr1 and arr1 should get changed?
Do this:
arr1 = ["a", "b", "c", "d"]
arr2 = arr1.clone
puts arr2
=> ["a", "b", "c", "d"]
arr1[1] = "z"
puts arr1
=> ["a", "z", "c", "d"]
puts arr2
=> ["a", "b", "c", "d"]
But if you change the whole arr1 to some other values then arr2 will not change.
Eg:
arr1 = ["a", "b", "c"]
arr2 = arr1
puts arr2
=> ["a", "b", "c"]
arr1 = ["z", "y", "x"]
puts arr2
=> ["a", "b", "c"]
arr2 will be same as old arr1 because arr1 memory locaton will be different after the new initilaization.
You can check this by arr1.id and arr2.id
In this case clone is not needed.
More about clone (copied from Ruby book(Dave Thomas, The Pragmatic Programmers))---
clone obj.clone -> anObject
Produces a shallow copy of obj---the instance variables of obj are copied, but not the objects they reference. Copies the frozen and tainted state of obj. See also the discussion under Object#dup .
class Klass
attr_accessor :str
end
s1 = Klass.new » #
s1.str = "Hello" » "Hello"
s2 = s1.clone » #
s2.str[1,4] = "i" » "i"
s1.inspect » "#
s2.inspect » "#
Saturday, December 20, 2008
Changes from Rails 1.2.3 to Rails 2.2.2
After a long time we have changed one of our project from Rails 1.2.3 to Rails 2.2.2 .
Steps to follow for migrating.
1. Get the latest Instant Rails
2. gem install rails (This will get the latest version (now its 2.2.2))
3. gem update --system (This will update your gems)
4. rake rails:freeze:edge (This will freeze your application to the new version. This will require svn installed)
Later start the server, then you will definitely face some problems with your old application.
The problems(with solutions) I faced while migrating are,
1. Error: undefined method `cache_template_extensions=' for ActionView::Base:Class
When you try to start the server, you will get this error.
cache_template_extensions is deprecated. so you need to remove config.action_view.cache_template_extensions = false in config/environments/development.rb
2.
*******************************************************************
* config.breakpoint_server has been deprecated and has no effect. *
*******************************************************************
This warning will appear, you need to remove config.breakpoint_server = true in development.rb
3. NameError: uninitialized constant Inflector
This will appear if your environment.rb or any other place including your old plugins consisits of
Inflector.inflections do |inflect|
end
This is actually deprecated. If you find any of these issues in plugins kindly update your plugins with your latest versions.
4. NameError: uninitialized constant Rails::Plugin::Dependencies
This will come if you use any old gems, Actually Dependencies are deprecated to ActiveSupport::Dependencies, so you can change this in your code.
These are the main issues i found while starting the server. But after starting the server you will find so many issues to fix. You need to fix them accordingly with Rails 2.2.2.
Steps to follow for migrating.
1. Get the latest Instant Rails
2. gem install rails (This will get the latest version (now its 2.2.2))
3. gem update --system (This will update your gems)
4. rake rails:freeze:edge (This will freeze your application to the new version. This will require svn installed)
Later start the server, then you will definitely face some problems with your old application.
The problems(with solutions) I faced while migrating are,
1. Error: undefined method `cache_template_extensions=' for ActionView::Base:Class
When you try to start the server, you will get this error.
cache_template_extensions is deprecated. so you need to remove config.action_view.cache_template_extensions = false in config/environments/development.rb
2.
*******************************************************************
* config.breakpoint_server has been deprecated and has no effect. *
*******************************************************************
This warning will appear, you need to remove config.breakpoint_server = true in development.rb
3. NameError: uninitialized constant Inflector
This will appear if your environment.rb or any other place including your old plugins consisits of
Inflector.inflections do |inflect|
end
This is actually deprecated. If you find any of these issues in plugins kindly update your plugins with your latest versions.
4. NameError: uninitialized constant Rails::Plugin::Dependencies
This will come if you use any old gems, Actually Dependencies are deprecated to ActiveSupport::Dependencies, so you can change this in your code.
These are the main issues i found while starting the server. But after starting the server you will find so many issues to fix. You need to fix them accordingly with Rails 2.2.2.
Labels:
migration,
Naga Harish,
Rails,
Rails 2.2.2
Saturday, October 25, 2008
Clearing the Email in the logs in Ruby on Rails.
Are your logs printing the complete email when you are sending using Action Mailer in ROR?
By default your Action Mailer will print the log when an email is being sent. So to Avoid this add the following line in envionment.rb where you initialize.
ActionMailer::Base.logger = nil
This should look like below.
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.logger = nil #this will prevent you by printing in the log.
So this will prevent by printing in all the logs.
By default your Action Mailer will print the log when an email is being sent. So to Avoid this add the following line in envionment.rb where you initialize.
ActionMailer::Base.logger = nil
This should look like below.
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.logger = nil #this will prevent you by printing in the log.
So this will prevent by printing in all the logs.
Labels:
ActionMailer,
Naga Harish,
Rails,
ROR
Saturday, October 4, 2008
Sending Email with attachment in Ruby on Rails using Gmail Account
Its very important to send an email with attachment in Ruby on Rails. Generally most of the applications use this.
I'll write you step by step procedure.
For sending an email you need to to specify the mailer details in environment.rb
require 'smtp_tls' #this is used for GMAIL . click here to get this file. Copy this file in your lib folder.
# ActionMailer Settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.logger = nil #this will prevent you by printing in the log.
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:user_name => "username@gmail.com",
:password => "your assword",
}
To use ActionMailer, you need to create a mailer model.
$ script/generate mailer SendMail
This will create the model file app/models/send_mail.rb
Where ever you want to call the mailer method,
SendMail.deliver_send_attachment(subject, message, document)
"deliver_ " will deliver the email specified with the method.
"send_attachment" method should be in send_mail.rb (model)
In you models app/models/send_mail.rb
class SendMail < ActionMailer::Base
def send_mail(subject, message, document)
@from = "your email"
@subject = subject
@body['message'] = message #@body["message"] here @message will be the instance variable
@recipients = "some email" #recipient email
content_type "text/html"
@sent_on = Time.now
unless document.nil?
part :content_type => document.content_type do |p|
p.attachment :content_type => document.content_type,
:body => File.open("./public/#{your file name}", 'rb') { |f| f.read },
:filename => document.filename
end
end
@layout = :some layout
end
end
In the above code for the attachment I passed document object, in which content type (image, doc, pdf etc.,)is saved.
generally the content type will be
"application/msword" -> for MSword
"application/pdf" -> for MSword
"image/gif" -> for gif images
Instead of document.content_type you hard code it by the type mentioned above.
eg:
part :content_type => "application/pdf" do |p|
p.attachment :content_type => "application/pdf",
:body => File.open("./public/#{your file name}", 'rb') { |f| f.read },
:filename => "file_name"
end
In your views app/views/send_mail/send_attachment.rhtml
Hi some name,
This is the message <%= @message %>
Yours truly,
blah blah.
This will send an email with the attachment using Gmail.
I'll write you step by step procedure.
For sending an email you need to to specify the mailer details in environment.rb
require 'smtp_tls' #this is used for GMAIL . click here to get this file. Copy this file in your lib folder.
# ActionMailer Settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.logger = nil #this will prevent you by printing in the log.
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:user_name => "username@gmail.com",
:password => "your assword",
}
To use ActionMailer, you need to create a mailer model.
$ script/generate mailer SendMail
This will create the model file app/models/send_mail.rb
Where ever you want to call the mailer method,
SendMail.deliver_send_attachment(subject, message, document)
"deliver_ " will deliver the email specified with the method.
"send_attachment" method should be in send_mail.rb (model)
In you models app/models/send_mail.rb
class SendMail < ActionMailer::Base
def send_mail(subject, message, document)
@from = "your email"
@subject = subject
@body['message'] = message #@body["message"] here @message will be the instance variable
@recipients = "some email" #recipient email
content_type "text/html"
@sent_on = Time.now
unless document.nil?
part :content_type => document.content_type do |p|
p.attachment :content_type => document.content_type,
:body => File.open("./public/#{your file name}", 'rb') { |f| f.read },
:filename => document.filename
end
end
@layout = :some layout
end
end
In the above code for the attachment I passed document object, in which content type (image, doc, pdf etc.,)is saved.
generally the content type will be
"application/msword" -> for MSword
"application/pdf" -> for MSword
"image/gif" -> for gif images
Instead of document.content_type you hard code it by the type mentioned above.
eg:
part :content_type => "application/pdf" do |p|
p.attachment :content_type => "application/pdf",
:body => File.open("./public/#{your file name}", 'rb') { |f| f.read },
:filename => "file_name"
end
In your views app/views/send_mail/send_attachment.rhtml
Hi some name,
This is the message <%= @message %>
Yours truly,
blah blah.
This will send an email with the attachment using Gmail.
Labels:
ActionMailer,
attachments,
email,
Gmail,
Naga Harish,
Rails,
Ruby
Monday, September 15, 2008
mobee.in @ Barcampbangalore7
Yesterday I attended Barcampbangalore7 at IIMB. This is the first barcamp I ever attended. Very interesting and I was excited to see the people talking on different things. I decided to give a talk for the next barcamp on any technical issues. :)
Our CEO Dashrath has given a talk on mobee.in . It was very interesting talk and the response is also very good. There was also some few issues and requirements came from the people. We may finish those requirements in another 2 or 3 days.. :)
Other than mobee.in discussion I attended few more discussions which are very interesting and its a very good sunday with all these discussions. The campus (IIMB) itself is very nice with lot of trees.
The lunch arranged by bcb7 is very nice and tasty. :)
Our CEO Dashrath has given a talk on mobee.in . It was very interesting talk and the response is also very good. There was also some few issues and requirements came from the people. We may finish those requirements in another 2 or 3 days.. :)
Other than mobee.in discussion I attended few more discussions which are very interesting and its a very good sunday with all these discussions. The campus (IIMB) itself is very nice with lot of trees.
The lunch arranged by bcb7 is very nice and tasty. :)
Labels:
Barcamp,
BarcampBangalore,
Barcampbangalore7,
bcb7,
emailtosms,
get emails on your mobile,
mobee,
mobee.in,
Naga Harish,
Rails,
Ruby
Monday, June 30, 2008
1 year celebrations in Bitlasoft
Officially Bitlasoft has celebrated 1 year completion on June 29th.
We had Black T-shirts with a logo. Its really awesome.
we had a long meeting with our CEO Dashrath Bitla and some of our colleagues with the PAST, PRESENT and FUTURE of the company.
we are looking more for the company from our beloved product ticketSimply. :)
We had Black T-shirts with a logo. Its really awesome.
we had a long meeting with our CEO Dashrath Bitla and some of our colleagues with the PAST, PRESENT and FUTURE of the company.
we are looking more for the company from our beloved product ticketSimply. :)
Labels:
Bitla Software,
Bitlasoft,
celebrations,
mobee,
mobee.in,
Naga Harish,
Rails,
Ruby
Subscribe to:
Comments (Atom)