When I first saw Ruby 1.9 new method Object#tap I slightly overlooked it and felt that this method may not be useful. But recently in one of my project I realized that how it can improve the reability of the code and avoids un-necessary return statements.
Rails offers two different way to implement the many to many association, this post will guide you to choose the right association for your project need
In both approach, the intermediate table is going to hold the relation and also will yield the same result but when to choose what, still puzzled?
Pointers to choose has_and_belongs_to_many. If you choose not to:
Access the model which holds the relationship
Have any validations for the association
Read the associaton through the relationship
To make it more clear, lets try to restrict the number of comments for a post.
This as well can be implemented in many ways but we (atleast I initally went with this approach) naturally tend to add logic in Post class
post.rb
12345678910
classPosthas_many:commentables,has_many:comments,:through=>:commentables,:before_add=>:check_comment_limitprivatedefcheck_comment_limit(comment)#If you want to stop comment getting added to post, you have to raise an error.raise'Comment limit reached'ifself.comments.count=5endend
Another approach to tackle this is to add validation logic to the Comment class
comment.rb
1234567891011121314
classCommenthas_many:commentables,has_many:posts,through=>:commentablesvalidate:check_comment_limitprivatedefcheck_comment_limitcomment_count=self.posts.comment.counterrors.add(:posts,'Comment limit reached')ifcomment_count=5#If you want to stop comment getting added to post, you have to raise an error.raise'Comment limit reached'ifcomment_count=5endend
In both the approaches we have to raise an exception to prevent the comment from getting added to the post. I felt these approaches are not the conventional way of doing things, I went back and read the rails guide on association and rewrote the validation logic in the relation model which holds the association.
Now, closely look at line number 5 we are returning the result from the block and this is passed on the enumerator for processing the next object in the array.
We can avoid this extra return statement with the help of Enumerator.each_with_object
I am starting the blogging journey with a big thanks to Brandon Mathis for his wonderfull blogging framework Octopress based on Jekyll
What I liked more about this blogging framework is the KISS principle. I can stick to my favourite text editor vim, it won’t ask you unnecessary question on configuring the website. Everything will be up by few commands (Hmm… yes actually you might need some ruby based coding knowledge to start using this blogging framework). The framework is well documented as well
Well…. thats it for now, will write more on how did I configured and installed Octopress for my personal needs.