More Rails Progress

I've been playing with Rails over the past few days and want to quickly express my experience so far.

Creating joins, lookup tables, tuning, grouping, sorting, and setting limits are all easy. You can do something like:
Actor.sum(:appearances,
:joins => "INNER JOIN appearances_show_lookup asl
ON asl.appearances_id
= appearances.appearances_id",
:group=>'asl.appearances_id',
:order=>'sum(appearances) DESC', :limit=>10)
which translates into
SELECT m.appearances_id, sum(appearances)
FROM appearances m
INNER JOIN appearances_show_lookup md
ON md.appearances_id=m.appearances_id
GROUP BY m.appearances_id
ORDER BY sum(appearances) DESC
LIMIT 10;
or...
Actor.find(:all,include=>:appearances_type,
:conditions=>"appearances_type.name='guest'",
:limit=>10)
to
SELECT *
FROM appearances
LEFT OUTER JOIN appearances_type
ON appearances_type.appearances_type_id =
appearances.appearances_type_id
WHERE (appearances_type.name='guest')
LIMIT 10
or expand it to 3 tables...
Actor.find(:all,
:include=>[:appearances_type, :show_scores],
:conditions=>"appearances_type.name='guest'",
:limit=>10,
:group=>'appearances.appearances_id' )
to
SELECT DISTINCT appearances.appearances_id
FROM appearances
LEFT OUTER JOIN appearances_type
ON appearances_type.appearances_type_id
= appearances.appearances_type_id
LEFT OUTER JOIN appearances_show_lookup
ON appearances_show_lookup.appearances_id
= appearances.appearances_id
WHERE (appearances_type.name='guest') LIMIT 10

SELECT *
FROM appearances
LEFT OUTER JOIN appearances_type
ON appearances_type.appearances_type_id
= appearances.appearances_type_id
LEFT OUTER JOIN appearances_show_lookup
ON appearances_show_lookup.appearances_id
= appearances.appearances_id
WHERE (appearances_type.name='guest')
AND appearances.appearances_id
IN ('5', '6', '7', '8', '9', '10',
'11', '12', '13', '14')
GROUP BY appearances.appearances_id
Since this final Ruby/Rails code actually produces 2 queries (and I'd prefer an inner join to an outer join as the default), I'm going to opt to create my own queries using find_by_sql like this...
Actor.find_by_sql "
SELECT m.*,
SUM(md.appearances) appearances,
mt.name type_name
FROM appearances m
INNER JOIN appearances_type mt
ON m.appearances_type_id
= mt.appearances_type_id
INNER JOIN appearances_show_lookup md
ON md.appearances_id
= m.appearances_id
WHERE mt.nominal = false
GROUP by m.appearances_id
ORDER BY appearances desc
LIMIT 10"
and produces identical SQL.

Even though you can override defaults for table names, primary keys and the like, I can't get the scaffolding scripts to work unless I set them to the Rails defaults (opinionated software!). I'm going to try and use the Rails Migrations tool for these updates. I'll be sure to provide updates on my progress.

Like many others, I love what I've experienced in Rails so far. It's a breath of fresh air from my previous Hibernate and Java experience. I love the ability to make a change then instantly test it using the Ruby console. It's really great! Now my concerns are shifting to performance and scalability.

Comments

Popular posts from this blog

I Believe...

FRail :include

Performance Tuning JCAPS - Part 2