Posts Tagged ‘database’

Installing mysql on Mac Snow Leopard

Posted in Uncategorized on September 18th, 2009 by ph-lee – 2 Comments

This articles is regarding Mac OS X 10.6 with mysql 5.1.37.

I followed this article at hivelogic.com which required compiling mysql from source. It mostly worked with a few hitches which I’d like to share.

After following the article and attempting to run my rails app the first problem I hit was…

uninitialized constant MysqlCompat::MysqlRes

this problem seemed to be that you need to specify the atchitecture as 64-bit when installing the mysql gem. So run the below command intead when install the mysql gem…

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-
config=/usr/local/mysql/bin/mysql_config

Or if you already installed the gem just re-install with this command. The next problem was the following…

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I’d move a project from ubuntu onto the mac I couldn’t find the mysql socket to use as it was specifying the wrong location. On the mac the file can be found at

/tmp/mysql.sock

So you’ll need to modify your database.yml file accordingly to point to this location

Hope this helps.

Gotcha : Mysql::Error: Unknown database 'application_name_test' /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'

Posted in Uncategorized on July 19th, 2009 by ph-lee – Be the first to comment

After dealing with thisGotcha : Mysql::Error: Access denied for user ‘root’@’localhost’ you’ve hit another problem of…

This means rails is looking for the test database but can’t find it cause it’s not there. Simply got to mysql Query Browser or command line, whichever you prefer and create a new database/schema of your application_name followed by _test so for example

my_app_test

Theres no need to copy/clone the database structure a across. Rails will replicate the development database as the test database.

Tip : Checking SQL generated by Active Record queries

Posted in Uncategorized on July 13th, 2009 by ph-lee – Be the first to comment

Those coming from PHP background or oher languages and have not had experience with object-relational mapping may at first not seem confident with the concept, still prefering to write raw SQL rather than using the tools available.

One tip I can give is to check the SQL queries that Active Record has generated and submitted to the databse. This can be done by viewing the console where the server was started (ie the command line where ruby script/server was executed). All SQL submitted to the database are displayed here when running in development mode. You can also check the logs in log/development.log, here all the text from the script/server console are stored for debugging purposes.

Hope this helps users become more confident in using Active Record rather than writing the SQL by hand.

How to : Use Ruby on Rails ActiveRecord to query multiple tables with joins and conditions

Posted in Uncategorized on July 8th, 2009 by ph-lee – Be the first to comment

This is a back-to-basics article I decided to revisit. Here we’ll demonstrate how to query two related tables using ActiveRecord with joins and conditions rather than having to write the SQL by hand. Instead we let Activerecord do all the hard work. I like to learn by example so let’s set the scene.

We’re an estate agent and have an application which stores client details and property details. There are many clients and many properties, a client can have many properties but a property can only belong to one client. A simple one-many relationship. Here are some quick models.

models/client.rb

1
2
3
class Client < ActiveRecord::Base
   has_many :properties
end

models/property.rb

1
2
3
class Property < ActiveRecord::Base
   belongs_to :client
end

Heres some information the above two tables may contain for the purpose of this example…

Client table structure

  • id (primary key)
  • first_name
  • last_name

Property table structure

  • id (primary key)
  • client_id (foreign key)
  • house_number
  • address_first_line

#Note that client_id is needed to keep a relationship between the two tables. This foreign key is handled by rails ActiveRecord. Since we specified the relationship in our models it will use client_id to look up primary keys in the client table (ie. client.id).

controllers/home.rb

1
2
3
4
5
class HomeController < ApplicationController
   def my_very_specific_query
      my_client = Client.find(:first, :joins => [:properties], :conditions => {:first_name => "Fred", :properties => {:house_number => 3}})
   end
end

The above is very specific query which suits the purpose of this example but rather useless in the real world. It finds the first client in the database (sort by id in ascending order by default) where their first name is “Fred” AND owns a property with the house number three.

:joins performs a INNER JOIN, (however if you need you can overide this by writing the exact join by specifying the SQL). This will let you specify conditions for your query which regard related tables.

:conditions lets you specifying the query conditions that will return when true. We have used a hash in this case specifying conditions from the model/table we wish to return (in this case Client) and condition in a related table (Property). Of course extra joins and conditions can be added to suit the need of your query. For further info please see the API on ActiveRecord

You should now be able to use ActiveRecord to query multiple tables using :joins and :conditions

Hope you found this useful.