My Experience with exists?, any?, and present?

A few weeks ago, I encountered a problem, which I think, really really weird!

How weird was that?

So, I have some tables with one to many relationship, let’s say table articles has many images and images has many image_sizes (the tables are different with the actual case). The system has a job that executes codes fetching Article load it eagerly with Image and ImageSize. We thought it was safe, since there were no complicated process at all. Just load at print it!

After a while, booooom! We had 500 in production……. Continue reading

Raw Insert Query with ActiveRecord

These past days have taken me to a place that I need to learn more about executing a raw query, without relying on generated active record object (existing model classes).

Last week, I wrote a lib, that I think, required me to wrote it with custom or raw query. My research was filled with these kind of results

ActiveRecord::Base.connection.execute(...)

I used it actually. But, until I find it hard to produce a query string that free from SQL injection. Anyone knows how to do it? Continue reading

Understanding Ruby Comparison and Control Flow Operators

This afternoon, I had a chance to discuss about something with my pair about the difference between comparison operators and control flow operators in Ruby. Yes! What is the actual difference between && || operators and “and” “or” operators?

I started Ruby around 4 years ago and I wrote a code like this

if (a == true and b == true)
  # my code
end

And I swear I won’t do that again! Continue reading

Redundant Emails with Devise

This afternoon, I was asked by my friend about how to make email column allow redundant data with rails and devise. Honestly, I never did something like that before and I couldn’t imagine what kind of case he was trying to solve with that particular design.

What I advised him was try removing the unique index attached to email column. That’s actually an easy task, just generate a migration file and make a little change so the migration file will be like this

class RemoveEmailIndexOnUser < ActiveRecord::Migration
  def change
    remove_index :users, :email
  end
end

Migrate it and boom! The index is now gone. In case, you still need the index, but don’t want it to be unique, you might have this code after the remove_index code.

add_index :users, :email

Continue reading

Addition for peek-mysql2

Hi Coders and Hello 2015!

Happy new year to all! Yeah, I know it’s already January 12, but it’s not to late to say that, since we’re still in January. :D

Guys, I have a new hobby now! (Oh please! Don’t you have anything else to tell people?)

Yes! Wandering around exploring github to find some awesome and cool project. One thing that I found interesting is peek. It’s like a bar that shows some helpful information related to your application performance. Queries performance is one of them. Continue reading

Questions That I Asked to Myself When Writing Codes

Lately, I’ve been involved intensively in Test Driven Development environment. Which is great! I’ve been waiting to have this experience of writing codes with clear standard. However, there’s no such thing of perfection (maybe I can talk about this later), but I give my highest respect to this new experience.

This cause some changes in how I write codes, to be precise, my attitude towards software development. So, there are more questions in my questions list, which I need to answer one by one, from the beginning of writing codes until the end. These are the complete questions:

  1. What’s the story?
  2. What am I suppose to do to solve it?
  3. Have I pulled new revisions?
  4. Do I need to create new branch?
  5. Do I need to write behaviour test?
  6. Do I need to write view test?
  7. Do I need to write controller test?
  8. Do I need to write model test?
  9. Do I need to write lib / other test?
  10. Do I need to write javascript test?
  11. Have I completed the story?
  12. Do I need to refactor?
  13. Do I need to extract the codes?
  14. Have I tested all related codes?

Those 14 questions are mandatory, in order to make sure that I always deliver quality codes.

Anyway, there’s other big question which should be asked in the very beginning.

Do I need CI?

 

Insert Post to WordPress via XML-RPC

Hello programmers!

I can’t count the days since the last time I added a post to my blog and here I am with a new post. This is about WordPress and XML-RPC.

Several days ago, I made a huge and gigantic mistake to the company’s server. I formatted the disk. Oh my God! How dumb I was for not reading the instruction thoroughly. The consequence, I erased some critical scripts which had been used to pull content and saved it to the database. Fortunately, I didn’t erased any critical data. Well, there will always a bright side in every trouble, right?

So what did I do? Of course, I rewrote the scripts. Those scripts are not mine, but at least, I get the logic of it. It’s actually read some XML files, parse it, insert it to a WordPress database through XML-RPC, and delete all the XML files. Easy right? Continue reading