Ruby on Rails


12
Oct 10

Purging Old ActiveRecord Sessions.

Using ActiveRecord::SessionStore has many benefits but the darwback is that sessions’ data can build up in the table resulting in 10,000+ entries if the application is moderately used, the easiest way to delete old stale sessions is by using the following code snippet.

class SessionCleaner
  def self.remove_stale_sessions
    ActiveRecord::SessionStore::Session.destroy_all( ['updated_at <?', 3.hour.ago] )
  end
end

Stick this code in a file called sessions_cleaner.rb in your /lib folder and execute is using the runner script, you can also schedule it using crontab, make sure you give it the full path though

script/runner -e production "SessionCleaner.remove_stale_sessions"

30
Jun 10

Handling Ajax Form Validation in Rails

Handling Ajax requests errors isn’t documented really well and there are many ways to accomplish this, some are simple and some are pretty darn complicated.

the easiest solution I found was rendering different partials depending on the status of the save or update methods, here is a snippet

  def update
    @bid = Bid.find(params[:id])

    respond_to do |format|
      if @bid.update_attributes(params[:bid])
        flash[:notice] = 'Bid was successfully updated.'
        format.js { render :partial => "update" }
      else
        format.js { render :partial => "update_fail"}
      end
    end
  end

It may not be the most elegant solution but is sure simple and does what it says it does.


6
Mar 10

Rails edit_polymorphic_path

This handy function comes in use when you don’t know that object type you want to edit, I used has_many_polymorphs to pull many objects related to my user object, here is the views code

<% @user.entities.each do |e| %>
<tr>
<td><%=h e.info.nameĀ  %>
<td><%= link_to 'Show', e %></td>
<td><%= link_to 'Edit', edit_polymorphic_path(e) %></td>
<td><%= link_to 'Destroy', e, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>

5
Mar 10

using Has_many_polymorphs

So I needed to establish a relation between one model and many other models, the User model can own many other models like Restaurants and Cafes, has_many_polymorphs comes to the rescue, check the plugin homepage for installation details.

Here are my models

class User < ActiveRecord::Base
  acts_as_authentic
  has_many_polymorphs :entities, :from => [:restaurants, :travel_agencies]
end

class EntitiesUser < ActiveRecord::Base
 belongs_to :user
 belongs_to :entity, :polymorphic => true
end

following is the migration for the EntitiesUser model

class CreateEntitiesUsers < ActiveRecord::Migration
 def self.up
   create_table :entities_users do |t|
     t.references :entity, :polymorphic => true
     t.references :user
     t.timestamps
   end
 end

 def self.down
   drop_table :entities_users
 end
end

And to make sure the links between the two models are being established I used the following code in the Entity model

  after_create :add_entity_user
  after_destroy :delete_entity_user

  protected
  def add_entity_user #add a relation between the entity and the currently logged in user
    user = UserSession.find.record
    eu = EntitiesUser.new( :entity_id => self.id, :entity_type => self.class.name, :user_id => user.id  )
    eu.save
  end
  def delete_entity_user #delete the relations for this entity
    eus = EntitiesUser.find(:all, :conditions => ["entity_id = ? and entity_type = ?", self.id, self.class.name])
    eus.each { |e| e.destroy }
  end

hope this helps.