Running Thinking Sphinx, with Mysql and Postgres on OSX Snow Leopard
Having installed Thinking Sphinx for a project that used postgres, I had trouble running it with mysql. I tried editing some configuration files, both for my project and even tried the global config for TS but to no avail, it kept complaining that it didn’t know the type = mysql.
This led me to think(coupled with the answer for my SO question) that I must’ve compiled my TS before specifically for postgres only so I set off to recompile said thing for both.
./configure ran fine though, but it had problems when I tried make, because it was complaining I wasn’t using the right architecture. I then realized the mysql installed in my machine was x86, when in fact what I needed was the 64-bit version.
I then downloaded the 64-bit version for macosx and installed. I then tried to make TS again and voila! It installed perfectly. Now I was itching to index my database, but a new error popped out:
ERROR: index 'prospect_delta': sql_range_query: Out of sort memory; increase server sort buffer siz
e
Using some google-fu, I finally nailed it down to this command:
sudo mysqld_safe --key_buffer_size=16M --sort_buffer_size=1M
which sets your buffer size a bit higher for the indexing. Whew!
Alphabar on Rails
I am working on this little project right now using Ruby on Rails and I ran into some trouble with the alphabar plugin. First, alphabar uses the with_scope method which, since Rails 2.0 I believe, has been protected, giving me some errors since I froze my Rails version. I was able to overcome this obstacle by using the send method instead. Just change the last part of the find method of the plugin from:
model.with_scope({:find => {:conditions => conditions}})
{model.find :all}
to:
model.send(:with_scope, {:find => {:conditions => conditions}})
{model.find :all}
After I finally got that to work the plugin was very useful, although the alphabar was limited to letters and blank which is useful if you don’t need numbers. To accomodate numbers just add this to the alphabar helper:
('0'..'9').to_a.each do |i|
slots << i
end
Theoretically it should work for any character but I haven’t tried it yet.
So there you have it. Hoepfully somebody having trouble with this plugin will find this post useful.
nl2br in RoR
I recently found out that line breaks are not recognized when displaying a large block of text on the view (e.g. a post body). In PHP this was handled by the nl2br method but I had no luck finding a similar function in RoR. So I did what any sane person would do, create one myself. I placed this method in our model to format a particular field but it seems a better choice to put it in a helper.
def nl2br(text)
return text.gsub(/\n/, ‘<br/>’)
end
And there you have it, your very own nl2br method. Either you just found something really useful or I just wasted 2 minutes of your time. If anyone has a better solution or if I missed a function for this let us know by dropping a comment.
Small But Terrible
I’m pretty sure most of us have been in a situation where we have been trying to fix a bug for hours and finally seeing that there had just been some tiny typo or maybe even a commented line of code. I’ve been through a lot of those experiences before, especially when I was still learning to code, and most of the time, I just breathe out a huge sigh of relied or just plainly laugh at myself.
Reminiscing about those ‘good old days’ could be fun too. Here’s some of those ‘what the hell/oh come on/I knew it was just a simple mistake!’ scenarios that I have experienced or just know of:
- A missing/extra space, comma, or whatever character in your code. When this happens, it usually takes a while before you realize it because most of the time, you assume that what you typed was correct. Especially with missing spaces, it really is hard to tell. I have had one experience before in coding in Flash, where my fellow coder had 3 extra spaces after the instance name of a clip. No wonder we couldn’t access the variable!
- Unpopulated database. I think Bit had experienced this too, when he was launching the AnimoAteneo.com site. I have also experienced this recently, where I KNEW my code was correct, but the reason why it wasn’t printing out anything was because it didn’t select anything(I was trying to select events during a week, which was stored in database…and the problem was there were no events that week).
- The semi-colon of emptiness. There are rare times in the life of a programmer(especially ones that still use the ancient arts of Java and other compiled languages) when s/he encounters the…wait for it…semi-colon of emptiness(SFX: thunder crash). This is one of the most frustrating things that can happen to you: accidentally put a semi-colon after an if statement. Although it has been used deliberately for empty if statements, some still accidentally put a semi-colon after the if statement, which invariably makes it useless. And for that, we have to thank those other languages that don’t require a semi-colon.
- HTML entities. These guys are a nightmare, especially with a database involved. tick-marks or whatever you call it are often confused with apostrophes, tabs(nbsps) with spaces, long dashes with short dashes(sounds weird but it does happen), etc. It is a real pain especially when the program or database doesn’t complain about it and you just find out about it after they have wreaked havoc in whatever you were working on.
- Uncommented/commented lines of code. This happens especially when you are using the “try and try ’til it works” approach in coding. This also happens when you are trying to debug your code and you forget to uncomment/comment some lines that subtly affect whatever your code was doing – like, say – assigning a static/hard-coded value to a variable.
These are just some of the mistakes that we can encounter while coding and minimizing the chances of these happening can help in coding a lot faster and having more time dealing with bugs that matter.