Bashar3A

Browsing: / Home
Shortlink

Solve the “You are trying to cache a Ruby object which cannot be serialized to memcached.”

By Bashar Abdullah on October 17, 2012 in Memcached, MongoDB, Ruby on Rails

I recently started using memcached with my mongoid database for Tathkarti. The site uses a lot of records, and I needed to cache collection of records. So I first tried something like this:

Rails.cache.fetch('teams', :expires_in => 1.day) do
Team.all
end

This resulted in the following error:

“You are trying to cache a Ruby object which cannot be serialized to memcached.”

This is because Mongoid, and Rails 3 in general,  lazy loads all queries. That is, initialization of an object is delayed until it is needed. Hence, Team.all in that case would only return Mongoid::Criteria in my case, and would not result in execution of the query and the return of the results you want cached. This may not be the best solution, but what I did was simply call .entries on the query like this.

Rails.cache.fetch('teams', :expires_in => 1.day) do
Team.all.entries
end
 This would execute the query and return an array of all the results.
Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

How to get started with MongoDB and Mongoid

By Bashar Abdullah on September 29, 2011 in MongoDB

For several years the talk have been increasing rapidly about non-relational schema-less databases, and I have wanted to try one out. For Tathkarti.com, I started with regular PostgreSQL, but I soon started collecting large amounts of data I wanted to analyze. I thought this would be a perfect place to test out a non-relational database. Less than a year after, I found my self faced with the task of restructuring and expanding my whole application and database. There is a lot of undergoing changes, and I needed something agile as I keep testing different options. Even Rails migrations did not seem so friendly for the task, so I decided to move everything over to a non-relational database.

One advise I got from Yehuda Katz at Railsconf 11 was to try out Redis. However, as promising as it is, I found that MongoDB has a gem, Mongoid, that better suits my needs, and will make the migration and code changes less drastic. My decision was also strongly influenced by the fact that Mongoid already supports Sunspot Solr  through Sunspot Mongoid, and is supported by Rails Admin, or so I thought at the time, until I realized Rails Admin does not work with it. The idea of nested documents sounded appealing as well, rather than relating documents that won’t be used but through their relative parent document anyways. The migration is still going on, so I have no records to show for, but so far I hold no regrets, despite some short backs I will explain shortly. I would still like to give Redis a go for some future project though.

Now to get started with Mongoid, you first need to understand what MongoDB is and how it works. For me, I got MongoDB: The Definitive Guide book to get me started. You don’t need to go through the whole book. Just the first 7-8 chapters to get going with just the development at least. The book is good for introduction, but it had some erratas that made me waste an hour or two cursing at the screen. So if you decide to get it, take a look at the book erratas once you feel something is wrong. If you’re an eBook hugger like me, you can get it DRM free from O’Reilly.

Next is Mongoid. I first went through a demo by Durran at RubyJax. It’s about 80 minutes long, and it serves as a nice introductory to both MongoDB and Mongoid. You will get some ideas about how to structure your data, when to use embedded documents and when to use relational ones and so. Then, there is always Railscasts with Ryan Bates.

This should have you ready to get going, and you can refer to the documentation for any assistance. One thing to note however is there are two formats to query and work with Mongoid. One is used all over the Mongoid documentation, and it goes like this:

Person.where(last_name: "Jordan")

I prefer to use the regular symbol form I’m used to in Rails.

Person.where(:last_name => "Jordan")

The main problem I faced during the migration was with Dates, which are not supported yet for querying, and you will need to convert all Date fields to Time. The other problem is sorting or querying for pre-epoch years (before 1970), which was not possible until recently with the MongoDB 2 release. I’m yet to test that part though, as I might have to upgrade Mongoid to the latest release, and this might break some other gems.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

How to get rid of annoying x & y parameters passed with input type image

By Bashar Abdullah on September 22, 2011 in Javascript

If you have a search form that submits by clicking an IMAGE INPUT like this

<form action="/search" method="get" name="searchForm">
  <input type="text" name="q" /> 
  <input type="image" src="/images/submitImage.png" />
</form>

and then you perform a search, you might be disappointed when you see the resulting url showing like this http://www.example.com/search?q=test&x=10&y=20. The reason is that when using image inputs to submit, browsers send the x & y coordinates of the click on that button when submitting. This really makes the url look ugly however.

The solution if you want to use an image is to use regular image, and handle the onClick event for that image like this:

<form action="/search" method="get" name="searchForm">
  <input type="text" name="q" /> 
  <input type="image" src="/images/submitImage.png" onclick="document.searchForm.submit()"/>
</form>

This will have the same form submit effect, but will not pass in the x & y parameters.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

ActiveRecord Callback Gotchas: before_save vs before_validate

By Bashar Abdullah on September 2, 2011 in Ruby on Rails

If you have an ActiveRecord object you need to perform some operation on before validating and saving it to the database, you would normally use the ActiveRecord Callbacks. But which callback to use might be a bit misleading for some, as it happened to me.

For my case, I needed to perform some trimming on a string, let’s call it name here, before sending it for validation and saving it. I looked at the before_save callback and thought that’s it. But that was not the case, for then, duplicate values were allowed in. Why? It’s because before_save callback is called after before_validation. So this is what was happening:

before_validation: #(nothing called here)
before_save: trim_name
#(Name is changed now)
save()

Obviously, what was validated is not what was inserted, and hence, to my disliking, I was getting duplicate names in the database. Solution ofcourse is to use the before_validation callback if you are altering the data and need to validate it.

Share this on: Mixx Delicious Digg Facebook Twitter

Search

Pages

  • About
  • Projects

Categories

  • Javascript
  • Memcached
  • MongoDB
  • Ruby on Rails

Archives

  • October 2012
  • September 2011

On Twitter

@Bashar3A

Copyright © 2013 Bashar3A.

Powered by WordPress and News.