Fishing with the Family
A short hike is just what the family needed
After a very busy summer taking a short one mile hike in our hills was a real breath of fresh air. We took the rods but didnt expect to catch anything. Then emily helped me land this slab of a cutthroat.

Twin Lakes Cutthroat
Upload CSV to multiple tables
Uploading a CSV to a single table is easy enough, but what if elements of your CSV go to separate tables and needed to be primary and foreign keys for each other?
I have two tables, Categories and Items. I have a CSV that looks like this:
Category One,Item One,Item one descriptions,,
,Item two,Item two descriptione,,
,Item three,Item three description,,
,Item four,Item four description,,
Category Two,Item five,Item five description,,
,Item six,Item six description,,
,Item seven,Item seven description,,
My controller method looks like this:
require 'csv'
def csv_import
@parsed_file=CSV::Reader.parse(params[:csv][:file])
@parsed_file.each do |row|
category = row[0]
if !category.nil?
c = Category.new
c.name = row[0]
c.save
@new_cat = c.id
end
i = Item.new
i.name = row[1]
i.description = row[2]
i.category_id = @new_cat
i.save
end
redirect_to :controller => 'category', :id => params[:csv][:item]
And my form looks like this:
<% form_for :csv, :url=>{:controller=>"category", :action=>"csv_import"}, :html => { :multipart => true } do |f| -%>
| Select a CSV File : | <%= f.file_field :file -%> |
| <%= submit_tag 'Submit' -%> | |
<% end -%>
Hosting on AWS part 2
Now i just want to run my rails app in production mode through apache instead of mongrel. Here is what I did.
First I needed to install apache.
yum install httpd
Then kill the existing mongrel process
pkill -9 mongrel_rails -u root
With apache installed I now have the directories I need. I moved my rails app from my webuser directory to my apache directory at /var/www/html.
Now i can start apache
apachectl start
If you go to your primary dns in a browser you should see the default fedora apache page.
Next I installed passenger
gem install passenger
Then the apache mod
passenger-install-apache2-module
Since I was using one of the pre-set AMI’s I am promted to install a few things.
* To install GNU C++ compiler:
Please run yum install gcc-c++ as root.
* To install Apache 2 development headers:
Please run yum install httpd-devel as root.
* To install Apache Portable Runtime (APR) development headers:
Please run yum install apr-devel as root
Then add these to the httpd.conf file
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby
And add a virtual host to the conf file the way described in the apache mod install instructions.
Then restart apache
apachectl restart
That was it. My rails app is now running through apache.
Hosting on AWS
Just some notes from setting up a Ruby on Rails instance on AWS EC2.
During the set up process of the instance you will be able to download a private key. Put that private key in a safe directory and chmod it to 400.
Then after completing the setup of the instance and you can see that it is running, go to your command line and enter:
ssh -i /path/to/key/[name of key].pem root@[public DNS]
That will get you logged in. You will need to go to /home/[webuser] to checkout your web project from SVN. But first you need to install SVN:
yum -y install subversion
Now you can check out:
svn checkout http://address.of.your.repository/and/path projectname
cd in to config and nano the database.yml, make the needed changes, then nano the environment.rb and edit the rails version if needed. Then make the database:
mysql
create database [whatever]
exit
Then cd into the root of the new project you just checked out:
rake db:migrate
And if you have fixtures to test with
rake db:fixtures:load
Amazon runs mogrel so to test your project you can add this shell script:
#!/bin/sh
cd /home/webuser/[project name]/log && rm mongrel.pid -f
pkill -9 mongrel_rails -u root
cd /home/webuser/[project name] && /usr/bin/mongrel_rails start -e development -p 80 -d
chmod that to 755 and run ./restart.sh
Go to the public DNS address in your browser and you should see your project.
Next, I will be attempting to run Rails through Apache on AWS with passenger instead of mongrel. For now this got my project online.
Caught between PHP and Ruby
Am I a PHP Developer? Am I a Ruby Developer? How about, I am a Web Developer.
This is something I have been discussing with various co-workers for some time. Learning Rails over the last couple of years, and now finally having more opportunites to use Rails I find that I have taken a similar path that Derek Sivers outlines in this article (http://www.oreillynet.com/ruby/blog/2007/09/7_reasons_i_switched_back_to_p_1.html), but I dont see myself abandoning Rails or PHP. I enjoy working in both and they both have their strengths and weaknesses.
I read something on another blog recently that has stuck with me. It read, “If the only tool in your toolbox is a hammer, everything looks like a nail”.
I finally used fixtures
I finally had a project where I thought fixtures and unit testing made sense and now I’m wondering why wasn’t I doing this all along?
I have been using rake migrations pretty much since I started using RoR. But default data hasnt been much of an issue on my projects until this last one. So I looked into fixtures and Im regretting not taking the time sooner to get to know fixtures better.
One cool thing I came accross was how to handle foriegn keys when loading fixtures. It kept choking on fixtures:load because by default it will load fixtures in alphabetical order. For example: My fixture for accounts has a FK of user_id but the accounts fixture will be loaded before the users fixture is. Thus my FK problem.
I found some clever ruby scripts that alter the ENV variables and so on but i thought a more direct approach was to just run it like this
rake db:fixtures:load FIXTURES=users,accounts
Pretty cool.
RoR Restful Authentication
I got sick of forgetting what to google to find this so I am keeping my notes here.
./script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication
Then run
./script/generate authenticated user sessions
You can add --include-activation to have add more features but i havent used that yet…. It works great as is.
Re-compiling PHP 5 for OS X
This post is intended as a simple reminder for me to help me remember what I did when setting up a new PHP environment… help yourself to my notes, but you’re on your own if it breaks your server.
Here is a breakdown of what to install in what order… jpeg6, gd, libpng, mcrypt, mhash
Build the jpeg6 lib first- ftp://ftp.uu.net/graphics/jpeg/
I put the tar files in ~/_bin then I unpacked it with
tar -zxf [tarfile]
then moved the dir to /usr/local/php. Then CD into that dir and do the following:
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
./configure --enable-shared
make
sudo make install
Then you should recompile GD so it will see the new jpg6. Same thing, download GD to the ~/bin and untar it and move the dir to /usr/local/php.
./configure
You should see something like this:
Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
Support for Fontconfig library: yes
Support for Xpm library: yes
Support for pthreads: yes
Then do this:
makel
sudo make install
Now you will need libpng (http://www.libpng.org/pub/png/libpng.html). Untar it and move it to /usr/local/php
./configure
make
sudo make install
Now you need freetype and xpm… Im not going to try and figure out fonts so since fonts are already on the system you can use them this way in the config for php in just a few minutes.
–with-freetype-dir=/usr/X11R6 \
–with-xpm-dir=/usr/X11R6 \
Now for mcrypt and mhash. Go here and download the latest version of mcrypt ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/. Same as the others, untar them and move them to /usr/local/php. CD into that dir then
./configure
make
sudo make install
And finally mhash. Go here and download it. http://sourceforge.net/project/showfiles.php?group_id=4286. Again, untar it and move it to /usr/local/php then
./configure
make
sudo make install
Now you are finally ready to compile PHP.
./configure --with-prefix=/usr \
--with-mandir=/usr/share/man \
--with-infodir=/usr/share/info \
--with-disable-dependency-tracking \
--with-apxs2=/usr/sbin/apxs \
--with-ldap=/usr \
--with-kerberos=/usr \
--with-enable-cli \
--with-gd \
--with-freetype-dir=/usr/X11R6 \
--with-xpm-dir=/usr/X11R6 \
--with-png-dir=/usr/local/lib \
--with-jpeg-dir=/usr/local/bin \
--with-tiff-dir=/usr \
--with-zlib-dir=/usr \
--with-enable-trans-sid \
--with-xml \
--with-enable-exif \
--with-enable-ftp \
--with-enable-mbstring \
--with-enable-mbregex \
--with-enable-dbx \
--with-enable-dbase \
--with-enable-trans-sid \
--with-enable-sockets \
--with-enable-wddx \
--with-enable-bcmath \
--with-iodbc=/usr \
--with-curl=/usr \
--with-config-file-path=/etc \
--with-sysconfdir=/private/etc \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mysql=/usr/local/mysql \
--with-pdo_mysql=/usr/local/mysql \
--with-openssl \
--with-xmlrpc \
--with-xsl=/usr \
--with-mcal=/usr/local/php \
--with-mcrypt=/usr/local/lib \
--with-mhash=/usr/local/lib \
--with-mime-magic \
--with-pear
I was able to run make fine, but when running make test I got an error…. After alot of digging around, I found this helpful post at php.net.
“After screwing around for an hour and screaming many profanities at my computer I looked back at the error and noticed:
/usr/local/mysql/lib/<b>mysql/</b>libmysqlclient.15.dylib
Why it was using this path I do not know, but I headslapped when I realised how simple it was! I solved the problem by creating a link called mysql that linked back on itself:
cd /usr/local/mysql/lib
sudo ln -s ./ mysql
Problem solved! I probably should have figured out why it was doing this instead, but this was easier and saved me from allot more frustration.”
This worked for me as well.
Reindexing Ferret Part II
Automating the re-indexing process was a bit different than I thought it would be.
Yesterday I posted that you can use script/console. Which is true, but you cant run that in a cronjob. Or, at least I couldnt get it to run in a cronjob.
So I learned about script/runner. I used it like this… I put a .rb script in lib/task called ferret_index.rb.
class FerretIndex < ActiveRecord::Base
MyModel.rebuild_index
end
Then my cronjob
0 1 * * 7 /usr/local/bin/ruby /usr/local/apache2/htdocs/<my project>/script/runner /usr/local/apache2/<my project>/lib/task/ferret_index.rb
Every sunday at 1:00 am my index will be updated.
Reindexing Ferret
Its really simple, wished I had seen this part of the documentation a couple months ago.
Lesson learned. As the databse was being updated more often and becoming very large, indexing became more difficult to do just by doing a search in the browser. Back to the documentation and there it was.
script/console
>>MyModel.rebuild_index
Plus, the indexing seems to be much faster. Looking into running the DRb server. Im worried about taking a performance hit. I’ll post what I learn when I try it out