on your active record model define has_many with dependent models.

i was refactoring our Item model, where we have 3 has_many with 3 mapping models.
as we are not using InnoDB based foreign key constraint, we were searching some sort of reliable solution,
which will take pressure in application layer instead of leaving it to the database.

so later we introduced “:dependent” with has_may relation. here is our top of Item model.
has_many :category_mappings, :dependent => :destroy
has_many :categories, :through => :category_mappings

has_many :property_values, :dependent => :destroy
has_many :properties, :through => :property_value

has_many :item_location_mappings, :dependent => :destroy
has_many :locations, :through => :item_location_mappings
our “dependent” flagship is destroying all related items in the item destroy process which has introduced
our flexibility and reduced a lot of code to manage such stuff in a DRY(ied) manner.

so the following unit test worked fine for us.

rails_dependent_unit_test

some bad side,
dependent delete each and every item one by one, which is big issue when you have a big chunk of dependent data.
but that is not suppose to be common in every context. we have no problem with this issue.

best of luck!

“work for fun”

attributes: rails reserved variable :(

yesterday, i had a pretty rough working day, i was stucked (along with my team) with some simple joining. we had the following models -

Attribute
———–
class Attribute < AR:B
belongs_to :category
end

class AttributeValue < AR:B
belongs_to :item
belongs_to :attribute
end

class Item :attribute_values
end

rails wasn’t giving much better error message, instead it was saying, “invalid type, String to Integer..”, so far i understood ActiveRecord stuff was trying to cast a string to integer.

we even didn’t know which field was doing this stupidity and so on…
so later we dug down to the rails scripts, we added few debug messages. we found “[]” was invoked to set “id” string value.

anyway, after digging more into to this issue, today i gave another try after 7 hours of nice sleep. it was about rails reserve words.

though i heard something from the rails community, but i was expecting some message or  errors which explain this issue.

anyway, i always like fail first approach. if something is not possible it should fail first. at least it should return a meaningful exception.

i believe Active Record should be patched with more clear warning or exception, if any reserved word is used for model or controller or others it should let us inform :) .

EiD recipe special : few conventions and best practice for software developer

i just gathered a few of my known conventions and best practices. i belief this will help many of us.

few of my previously written posts -
usages of “final” keyword in java -

http://www.somewhereinblog.net/blog/hasan/28708715

usages of comment and coding
http://www.somewhereinblog.net/blog/hasan/28704020

let’s check out my recipes -

1. code from your peer developer perspective view.
while you writing your code, always think your code will be reviewed by some other developers, who is not like you,
who is very serious in simple fault and ignorance in writing code. your code will reflect who you are, so better be careful while you exposing yourselves (through writing code).

2. variable convention -
never write short variable name, always write variable which reflects it usages.
for example -

$user = new User() instead of $u = new User(). follow same convention when you have nested “loop”

3. source file naming convention (except java)
set your file name in such a way that will reflect the usages of the file.
for example -

verify_user_authentication.php (or in a packaging manner – user_util_verify_authentication.php). in java you better know how to write package.


4. keep your url domain centric

all urls should be inherited by some specific problem domain for example -

http://abc.com/user/login

http://abc.com/user/register

http://abc.com/user/update

here “user” is problem domain and “login|register|update” are actions or you could say problem.

5. method naming convention -
write method name in such a format that will reflect it usages. or simply make it self describing.
for example -

function verify_user_credentials() {}
def verify_whether_user_profile_is_complete() {}
private void checkUserFlyLimit() {}

6. don’t write query from controller:
build or execute query from DAO or helper or utility or domain itself. don’t put it over controller which limits the reuability and later extendability and interception

7. single concern
while coding, keep your method slim and less concerned about other implementation. for example -

private void isAuthorizedUser(pUser, pAction) {
if (userAlreadyLoggedOn(pUser) && userIsNotBlocked(pUser) && userHasPermission(pUser, pAction)) {
// boooooooooooooooomm…
}
}

8. think from testability
while writing your code always think from testable perspective view. this means, how you could testify your work or module
or bunch of code which your company is paying for.

9. follow coding norms -
i. think before coding

whatever you want to put on your code try to think or imagine before start coding.

ii. dump your think in comment

whatever you thought just write it down over comment or paper or in a common place where you or future developers could have look on. (better place in comment)

iii. write your algorithm in comment.

whatever you planned write in comment before kick start your coding.

iv. set up your unit test case.

find all probable assertion points from use case, put them over your unit test case.

v. write your code
vi. coding routine -

(1) test -> (2) code – go to > (1) [recursively]

10. always put your tag over the source file,
whatever you wrote, that reflects your confident and work efficiency, so better you tag your name over every source code you have written or modified. good for tech lead or project manager, because tagging each file with author name, it creates a hidden responsibility for the author :) good for both parties.
for example -

/**
* @author someone
*/

where to invoke “layout”? AppllicationController or ChildController?

these days i am having a lot of fun with Ruby on Rails. it seems to be like an open ground with lot of handful tools, now come up with ideas and build them up.

handful ruby based tools really helping me to let my smile back. anyway, while i was writing code on rails i am feeling something can be better practice something is not good. from now on, i will try to put them over the my blog.

their is method called “layout” which is used to define the based layout for the controller. this can be used from ApplicationController and other domain specific controller for example : “UserController”.

my team and me was used to define layout over the domain specific controller, but i belief, as we are repeating same “layout ‘template1′” code over and over, it must be better to keep inside the ApplicationController, as because all domain specific controllers are extending application controller, more over it is easy to change.

you may get a question about ajax based response, the better answer is, use render .., layout => false, that will get rid of base layout.

best wishes,

configure rails in production environment.

for last couple of days, i was struggle with rails deployment related issue, so far i was happy with mongrel backed and apache reverse proxy front configuration.

so last night, i found mongrel is surprisingly slow for serving static content, like javascripts, images, and styles files. so later i set apache for serving static files. now it seems out performing ;)

anyway, i was wondering about mongrel and how does it works in production deployment. as we know (who use java backed application server in production environment), any java backed application container are configurable. where we can define the maximum limit of thread pool size.
but i didn’t find this type of thing over mongrel, later few of the following urls, really helped me to understand how mongrel  works in production environment. the concept is very simple.

every mongrel worker is a standalone instance of mongrel and every mongrel instances are listening to a specific port. so i have to setup a reverse proxy or a load balancer which will be used to pick up the right mongrel instance.  for all session affinity shake, there must be some solutions, this is what i have to figure out later. (hopefully after getting my laptop)
here are the resource links -

pound – used as load balancer and reverse proxy service.

jared’s guide how to install pound

Grails vs rails fight, though it doesn’t worth rather than few configuration learning.

hope these will be helpful for my future reminding :)

Just bang, a new group, Rails artist, Only Ruby on Rails Artist here

these message convey the basic purpose of the rails_artist group-

Those who belief on themselves, those who believes they are passionate about the rails stuff in ruby way on rails, i belief this gonna create a new open space for them to share their nut and bolts.

from now on let’s change your title from developer, software engineer or architect to an ARTIST :)

welcome, dear artist.

here is the group url -

http://tech.groups.yahoo.com/group/rails_artist/ 

ruby: user friendly url, slug

i had a requirement to create user friendly url, like the following example -

www.domain.com/user-friendly-url,

so you know i am getting more and more passion over ruby stuff, http://blood.we4tech.com just brought me on rails again.

here is the ruby code snap, which removes all kinda illegal characters and  creates a nice slug -

title.gsub(/[^[:alnum:]]+/i, “_”)

thanks  push.cx for quick nice example

best wishes,

IntelliJ IDEA and ruby plugin

i belief, i should share this story, once i heard about apatana (eclipse based) ruby IDE i gave it a try without being late, later i heard Net beans got ruby supported, again i wasn’t late and gave it a try.

as you know, i am java developer, always like java stuff everywhere (if possible). anyway, later i returned to my favorite IDE IntelliJ idea.

my personal judgement in terms of usable and flexibility i must say, intelliJ idea with ruby plugin just rocks!!
currently i am developing a active record kind of things where i am using java based repository model in back end and my restful web service clients are developed on ruby.

i am pleased with intelliJ idea, as because the ruby plugin is too friendly, though i faced lot of internal code hints related problem, but i could see those problems were fixed on new release.

here is the ruby plugin details page

damn simple!!!, define instance variables in ruby way

i was creating a new class where a hash object will be passed with value and key. my class suppose to iterate for each key and value. and those key will be treated as the instance variable.

so the pretty simple job i did is the following lines of code block -

def initialize(p_params = {})
super()
unless p_params.empty?
p_params.each do |key, value|
eval %(
self.#{key} = value
)

end
end
end

surprisingly my instance variable declaration became soooooo simple and easy.

update:
i didn’t know that self.send(“#{key}=”, value) does the same job, obviously send is prefer way to do this job.

Wow, The “Ruby on Rails with Oracle” FAQ

Wow, ruby with Oracle XE

Exploring Ruby on Rails (RoR)? This new FAQ provides basic information
about connecting to Oracle, installation, and creating Oracle Database
XE + RoR applications.

here is FAQ

powered by performancing firefox

Technorati Tags:

my tweets

 

February 2012
S S M T W T F
« Aug    
 123
45678910
11121314151617
18192021222324
2526272829  

Flickr Photos

@kamalapur over bridge

@kamalapur station

cox's bazaar trip oct 09

cox's bazaar trip oct 09

cast ur vote!

More Photos
Follow

Get every new post delivered to your Inbox.