Monday, June 16, 2008

ActiveRecord Association Weird Length Method

The method 'length' defined for ActiveRecord associations works in a very weird manner.

consider this example


class User < ActiveRecord::Base
has_many :feeds
end


i was using the class and writing this snippet of code to get the count of feeds in the db


@user = User.find :first




puts @user.feeds.length


looking at the log, i found that the added query was


SELECT * FROM `feeds` WHERE (feeds.user_id = 1)


and not


SELECT count(*) AS count_all FROM `feeds` WHERE (feeds.user_id = 1)


so in order to get away from such weird sql statement which will exhaust a very large bandwidth getting all records to count them, use this instead


puts @user.feeds.count


or


puts @user.feeds.size


but don't use 'length' method

Thursday, May 15, 2008

SVN Branches

Imagine working on a deployed project, and you want to add some new features and changes on the running copy.

Since making new features may cause problems in the deployed project, you need to have another copy from the application and work on it freely then merge it with the running copy to apply your changes.

Subversion makes this very easy, all you need to do is to create a new branch.

So, what is a branch?

in order to understand that, lets look at the repository location hierarichy, you will find that it is as follows
--
-- trunk
-- branches
  • trunk is the folder containing the version of the website currently deployed

  • branches is another folder containing some other versions, as a new edition of the application
Now, in order to do that, just run this command

svn copy ((repository-path))/((project-name))/trunk ((repository-path))/((project-name))/branches/((branch-name)) -m "Creating a private branch in order to ... bla bla bla"


now you are done with your branch, checkout the project from that new path, and do any changes you want freely then at the end merge with your trunk to apply your changes

Monday, May 5, 2008

acts_as_changed plugin farewell

i used acts_as_changed plugin since a while and i have been enjoying the idea of being capable to know which attributes has been changed in my model and to take action upon certain attributes changes

and today i only tried to look at it again to know if there are any changes made in it or not but, i was shocked when i read a comment from the owner stating that it isn't plugin anymore

it is now an alternative base.rb file that you can update your activerecord with if you want to enjoy the plugin functionality once again

and he stated that he did so because he was facing problem in callbacks that he couldn't solve till now except through this way

anyway, i hope he finds a solution for it so that we all benefit from this plugin functionality

Wednesday, April 23, 2008

Memcache Trick

i was faced with a very weird behaviour while working with memcache-client with two memcache servers

first lets look of my configuration to the client in order to explain on it
On my first server

memcache_options = {
((some options)
}
CACHE = MemCache.new memcache_options
CACHE.servers = [((SERVER1)), ((SERVER2))]


On my second server

memcache_options = {
((some options)
}
CACHE = MemCache.new memcache_options
CACHE.servers = [((SERVER2)), ((SERVER1))]


after running both servers, i noticed that what i enter in memcache on that server isn't available to the other server

Where is the problem??

here it is, a very strange reason

CACHE.servers = [((SERVER2)), ((SERVER1))]
CACHE.servers = [((SERVER1)), ((SERVER2))]


this is the problem, one uses SERVER1 then SERVER2
other uses SERVER2 then SERVER1

i thought that memcache client when checking for a key, he looks at the first, if not found then looks at the second then say it is a miss
but i realized that if it isn't on the first then it doesn't look at the second

That's it, just take care, cuz small things can waste your time and energy looking after their reasons

Adding Data Through Migrations

one of the points i am always faced with while adding some migrations, is my need to add some system data as a static group for Administrators or any other.

so simply i create a new migration and add the following
ex:
Group.create((some_attributes>))

and it simply works fine, until someone later one adds a new column
in another new migration and adds validation on that column in the Model

and here the problem occurs
my old migration throws exception because of validation added on a new column added later in another migration but Model carries code validating it

how to solve these problems?

i suggest not using create at all
but rather use new and set our attributes
then save without validations

Thursday, April 3, 2008

Thick Models Thin Controllers

i have worked for about six months with Rails and through this time i was coding a lot in my controller which has shown to me lots of problem later on

for example,

i was having a Topic model and Post model, where every topic has many posts
and in each of these two models there is a field called archive

i was having a controller that changes the archive field to a certain value then pass on every post it owns and change its attribute also

later on, i made another controller and in it i also changed that field in it and passed on all posts to do the same change in it

this led to redundancy and duplication of code

but the problem became more complicated when i wanted to change the logic of this topic and its posts replication to a new way

here i have to move on my controllers and change this code in them

but i have added the code that observe this change in topic attribute and apply it on all its posts inside the model, i would haven't faced this problem anymore
and this will give me flexibility to do changes easily also

so this led to the idea of having my controller thin containing the least amount of code and my model thick containing large amount of code

Wednesday, March 26, 2008

script/console reload models

if you are one of the Rails applications developer, then you would have used this command before
ruby script/console

it is very nice and gives you the ability to load your environment and try it as you wish, but what if you changed anything in the application models, you will find that the console isn't aware of these changes at all

so what can you do to make him aware of your changes?

here it is, just run this command in your console
Dispatcher.reset_application!


Now you can try the new changes you made in your models