Category Archives: rails

Session updates During concurrent requests


So this has always been a problem with Rails SessionStore. The problem is that if your app is sending concurrent requests and one of them is updating a session then depending on which request wins in the race you might get the stale session variables instead on the updated one. The problem is better explained at other places like
http://dev.rubyonrails.org/ticket/8256
and the solution is a plugin smart_session_store by Frederick Cheung.

It might not seem a very informative post to you, but I am still posting it as this has taken me quite a bit of time to find out. And after finding it, I am astonished to see why Rails still can not deal with this issue.

 

Note: I am talking about Rails 2.3.8 here. Not sure if Rails 3 has a fix for it or not.

 


Move towards optimization now…


If you are looking to do the query optimization of your rails apps, I can suggest using few tools that does it.

1. First I want to suggest BULLET- A rails plugin/gem to kill N+1 queries and unused eager loading.

Bullet helps in finding the space for eager loading. It has a beautiful way of telling where should you add the eager loading for certain association. Whenever you try to run the code in development browser, it pops up a js alert and tells you exaclty where and what includes you have to add in you finder queries. It also tells you to remove the extra unnecessary eager loading handled in you app.

To add more beauty to it flyerhzm added growl notification as well for the Mac based developers.

2. Second suggested tool will be Slim-Scrooge: SlimScrooge heavily optimises your database interactions

Slim-Scrooge works for optimizing the sql queries by monitoring the required columns for the model and changing inline the queries to fetch only selective columns. So with slim_scrooge working in background you don’t have to worry about giving the options to :select in you find. Instead slim_scrooge will itself analyze what are the columns needed to run your code for the first time and second call to the same action it itself puts the necessary column in select removing the asterisk(*).

I am still skeptical about using it. As it might make the developers lazy(just blindly putting * in queries) and increasing the processing time in slim_scrooge inline query modifier. Though I think it could be a good tool if used once in a while to analyze what are the missing selects in our code. Whatever be the case you might wanna give it a try, its kinda lucraitve.

3. Last but not the least I try to add is rack-bug: Debugging toolbar for Rack applications implemented as middleware.

Remember rails-footnotes? Well Rack-bug is kinda same but seems better and provide more. Better, because its rack based. Provides more with the toolbar giving following info:

  • Rails Info
  • Resource Usage
  • Rack Env
  • SQL
  • AR Objects
  • Memcached
  • Templates
  • Log
  • Memory Usage

I kind of loved it at first sight. So if you already have footnotes. Give it a try. If you dont, then again give it a try to optimize better. Watch the screencast by brynary for more info.

If you have tried any of those feel free to give your opinions. I will be happy to learn how everybody is approaching the technique of optimization.



Merb Slices in Rails 3: The goodies coming along more efficiently


As most of you already know Merb Slices are the mix of merb app and plugin, which carries feature goodies of both. Its the part of an app which can itself work as an application and can be incorporated in any other merb application. For example remember Rails engines. It gives the same functionality but is 100 times better that that.

While Merb is merging into Rails. In Rails 3 we will be witnessing this remarkable feature of Merb in Rails as well. If the planning of new core team goes well...We will be able to mount any of our rails application in another very easily. Core team has given a tentative date for Rails 3 by next RailsConf in May. I can not wait to explore all these  good features. Just keeping my fingers crossed…


Merb + Rails == Great Software


So you heard the news??? Yes, Merb and Rails are going to merge in Rails 3 If thats true it will be a great merger o all times in ruby community. The largest framework of Ruby glued with the fastest one will be cool.

One of the thing which I loved in Merb is its agnostic feature. Specially ORM to quote. As I found Datamapper to be more efficient and useful then ActiveRecord in Rails. If we can use Datamapper in Rails it will be the coolest thing.

While trying Merb I also felt like most of the things are implemented in the same way as in Rails. So the basic difference is just in the values Merb advertise or rather built on …

* Being Agnostic

* Prefer Plugin

I am just waiting for Rails to adopt those values so it could be more efficient and more lovable :)


Ruby Funday: Live – Part 2


Sur’s PURE CODE PRESENTATION went very well. Had a lot of discussion on Ruby not needing the Type Casting which leads to a lil bit doubt coming up in JAVA guys interested in Ruby.

Afterwards we had a real good session by Aditya from Impetus. Aditya talked about the new product they are gonna launch very soon. Its a deployment tool written in C+Ruby Combo. The good news about it is its Open Source which increases its scope widely for future.

In his talks Aditya point out some main features of his deployment product. Which he is calling “ATLAS” right now and is in pre alpha version. Some main aspects they have focussed upon while developing are as follows:

  • Runs Rails and Rack compliant applications.
  • Multiple web application support.
  • Intelligent load balancer.
  • and many more to list…

The presentation that followed Aditya’s was Sid from Vinsol. He presented on Internationalization in Rails 2.0. Which was pretty state forward and funny when you see the English got converted in Hindi statements. He compared the Internationalization in Rails prior to 2.2 with Gibberish plugin and the new Rails i18gem implementation. The i18 implementation makes it pretty simple to implement. It has the built-in Active Record error message internationalization suport which was missing in previous release of Gibberish. So thats another cool feature of Rails 2.2

Talking about Rails 2.2. Its officially out just few hours back. Check out http://explore.twitter.com/d2h/status/1017059364.

I am back after Lunch now. And Gaurav is Presenting on ‘Air on Rails’. Check out next post for that.

I ‘ll be back again. :)


Now steriods in Plugins…


Wow cool idea ;) … And the plugin i am talking bout seems promising too. The plugin of the day for me is acts_as_taggable_on_steroids. The writer says it enhances acts_as_taggable by DHH with features like Caching and tagcloud calcultions. Although i could not use it till now, but i m planning to check it out in my new project as soon as the first phase completes which seems to be very near :D


Strange behaviour of respond_to in IE


In recent days i have monitored a very strange behaviour of respond_to in IE.I am sharing my experience in hope that somebody might explain it to me. As i cudnt find anything related to this from my best teacher google :(

I am using edge rails in one of my projects. some where in that i have used respond_to in following way…


def show
respond_to |format| do
format.js {render :partial=>'show'}
format.html {}
end
end

When i checked the response in firefox for html and xml both requests type, it was perfect. But when i checked html response in IE 6 creates a blunder :O.It was returning a js file with the javascript response.

But when i changed the code and placed the html call first and then checked xml call it worked fine in IE too.

def show
respond_to |format| do
format.html {}
format.js {render :partial=>'show'}
end
end

Now can somebody tell me why is this happening and anyone else faced this issue ever?


Ruby/Rails Music album


Its melodious …its amazing and i can keep humming one or other track all the time. Really i loved it :)

Chris has the music for Ruby/Rails Community.He calls is IRB Mix .Tape and its just fantastic.

Track 4 about sandboxing is just mind blowing…

$ ruby script/console --sandbox
Loading development environment in sandbox.
Any modifications you make will be rolled back on exit.

And Track 5 about underscore operator (_) .I hadnt found yet anything in irb giving the value of last evaluated expression in Ruby. But ‘_’ does this cake thing for me. Good job Chris :D

One more small thing that i learned from one of the comment….

>> 4*4
=> 16
>> 4**4
=> 256
>> 100**100**100
c:/ruby/lib/ruby/1.8/rational.rb:543: warning: in a**b, b may be too big
=> Infinity
>>


Pdf-writer with login engine problem


If you are using pdf-writer with login engine, you can encounter a nasty problem with transactions in login engine like me(gosh… this was crazy). This is because login engine uses transaction-simple module (which is a part of transaction-simple by  Austin Ziegler but not the whole gem) included in rails 1.1 in active record.

While including pdf-writer it require the whole transaction-simple library.So in such a case the simple.rb file is required twice in the application.Which creates a problem in registration in login engine as it uses tranactions.

This ticket is discussed in here and a patch is also available…


http://dev.rubyonrails.org/ticket/4732


http://dev.rubyonrails.org/ticket/4932

Bad news is it can not be fixed before rails version 2 according to DHH ‘s response[:(]


Simple star rating in Rails


Those who wants to include simple 5 star rating mechanism in their ROR application.Can include a couple of functions in their helper to provide a form a rating field and display the result.Of course no 1/2 ratings can be achieved through this way. One needs to include the below given two function to some helper. And in view inside form tag call…


  star_rating_field("object", "fieldname", options)

Options are location, prefix, active_img, grey_img, hover_img…

For displaying the result of the rating in star images format…call from view


  img_tag_for_star_rating(rating_value,options)

Options are location, active_img, grey_img, hover_img…

Download the function to include in helper from here …I am finding it very difficult to put the code in wordpress without tampering so better to put it somewhere else ;)

Update: the file gets removed everytime.I m trying to out the code here only...


  #General function for star rating
  # Use these function as helpers
  # No half ratings are handled here
  # options can hav ...
  # location=>location of images( you need to have star_active.gif, star_grey.gif, star_hover.gif in this directory or provide names of image file in options)
  # prefix => prefix to append to each ids in DOM for descrimination of more then one star rating in the same page
  # grey_img => name of inactive/grey image of star
  # active_img => name of active image of star
  # hover_img => name of hover image of star
  def star_rating_field(obj_name, field_name, options={:location=>'/images',:hover_img=>"star_hover.gif", :active_img=>"star_active.gif", :grey_img=>"star_grey.gif"})

    # append a hidden field to store value of viz system field
    # this field is to be changed as hidden after testing
    hid = text_field(obj_name, field_name)
    hid_id = "#{obj_name}_#{field_name}"
    pref_id = options[:prefix].nil? ? 'star' : options[:prefix]+'_star'
    img_location = options[:location]
    # set initial value by javascript
    jscript = ""
    jscript += "var a; var v = parseInt($('#{hid_id}').value); for(var i = 1;i"
    img = ''

    # add images to field
    for i in 1..5
      img += ""+' '
    end
      return  img + hid + jscript
  end

  def jscript_for_star_rating(pref_id, img_location)
    jscript = " function set_star_status(n, id)    {  if( $(id).value != n){ for(var j=1; j"star_active.gif", :grey_img=>"star_grey.gif"})
    image = ''
    for i in 1..5
      if i "
      else
        image += ""
      end
    end
    return image
  end

Note:Do remember to include prototype library from script.aculo.us in your page.

UPDATE2:phew…finally trying to add the file again.Hope it work fine this time :( try this=>Star Rating functions


Follow

Get every new post delivered to your Inbox.