
Ask HN: How can I learn "advanced" data modelling for Rails? - rman666
I'm a beginner (hobbyist) Rails developer. I've gone through many of the available tutorials, such as Michael Hartl's excellent Ruby on Rails Tutorial at http://ruby.railstutorial.org/ (both editions, books, and screencasts). I can build and understand simple CRUD-type applications with associations with no problems.<p>Now, I want to "scratch my own itch" and build an application I can actually use in my work. Basically, it will need to be able to keep track of tasks and comments arranged as projects and project templates. I'm running into difficulty designing these models and associations because they are more complex. I'm sure I could find or buy an application to do this, but I'd really like to learn how to do it myself, instead.<p>Question: Are there techniques or resources (books, tutorials, articles) or forums where I can learn to design more complex models and associations? This doesn't seem like it's specific to Rails (to me). Should I be researching database techniques, models, associations, for example, to get ideas?
======
chanks
I love Rails, but I strongly disagree with its philosophy of the DB as being
just a dumb data store. Two things I would recommend.

One, the book Enterprise Rails. It's a few years old, so all the examples are
from Rails 2, but the meat of it is about proper database design, including
the stuff that Rails doesn't believe in (composite primary keys, foreign key
constraints, check constraints, triggers). Chapter 4, about the importance of
data integrity, was probably the single most useful thing I've read about web
development in the past few years.

Most of it is on Google Books (although sadly, chunks of chapter 4 are
missing):
[http://books.google.com/books?id=thTju-4duY4C&printsec=f...](http://books.google.com/books?id=thTju-4duY4C&printsec=frontcover)

Second, the best thing I did was replace ActiveRecord with Sequel. Sequel has
built-in support for all of those things that ActiveRecord doesn't (I couldn't
live without its composite primary key support), and is in general a much more
robust library than ActiveRecord. It also supports more exotic SQL features
like CTEs and CASE statements without forcing you to drop down to writing raw
SQL (though you can certainly do that if you want).

It's not a necessary upgrade for everyone. If your app has fairly simple
models and relationships, ActiveRecord will probably be just fine for you. But
if you want to get better with SQL and get closer to your DB, I think Sequel
is a much better choice.

More info on Sequel: <http://sequel.rubyforge.org/>

It also integrates very well with Postgres - there's a C extension for the
Sequel postgres adapter (<https://github.com/jeremyevans/sequel_pg>) and
support for adding common triggers in migrations, counter columns and whatnot
(<https://github.com/jeremyevans/sequel_postgresql_triggers>). It also
supports the more exotic datatypes - hstore, arrays, even the json and range
types that are coming in PG 9.2.

~~~
damoncali
This is overkill for someone looking to grow into intermediate relational
database concepts.

~~~
bartonfink
What would you describe as intermediate relational database concepts?

~~~
damoncali
Based on what the OP says he wants to build, I'd say things like joins, the
out of the box many-to-many functionality, etc. Basically, how do you link
your models together in a sane way. No need for ORM replacement. Just good SQL
fundamental knowledge.

<http://sql.learncodethehardway.org/book/> would be a good start.

------
sk55
From CMU's Application Design and Development course which uses rails:

Sample projects with good comments: <https://github.com/profh/arbeit-S12>
(project management app the professor created)
<https://github.com/profh/PATS_67272>

Slides from the class: <http://rook.hss.cmu.edu/~67272/schedule.php>

~~~
t4nkd
ಠ_ಠ This pattern is everywhere...
[https://github.com/profh/arbeit-S12/blob/master/app/models/a...](https://github.com/profh/arbeit-S12/blob/master/app/models/assignment.rb#L19-24)

~~~
mapleoin
but it's readable, right?

------
ispivey
SQL For Web Nerds is a terrific resource for learning SQL.
<http://philip.greenspun.com/sql/>

If your data model is built on top of an RDBMS, learning how one works is the
best first step to designing good data models!

~~~
rman666
Excellent resource. Thanks!

------
marcelojnk
I totally get what you mean. When I started with Rails I felt the same way.
First I've completed some tutorials and read Agile Development With Rails but
at that time i could only build "standard" applications - I couldn't think out
of the box.

That happens probably because you don't yet understand much Ruby and how Rails
use it to do all its "magic". My suggestion is that you keep trying. But here
are some tips:

Learn more Ruby; watch every episode of Railscasts; dive into the source code
(even if you don't undertand it); read open source apps on Github like Spree;
build as many apps as you possibly can;

~~~
rman666
Thanks, marcelojnk. Looking at Spree now.

------
jiggy2011
Difficult to know what you mean specifically by "advanced" relations.

Might be worth looking at SQL itself first so that you understand what goes on
inside the ORM.

Get comfortable doing join , inner join , outer join etc across multiple
tables.

~~~
rman666
Thank you. After reviewing a few of the answers, I realize this (better
understanding of RDBMS) is where I need to focus.

------
joshcrews
I went through a similar growth curve of being a total beginner to full-time
Rails developer.

For tips: there's a lot of power in has_many :through. Watch the relevant
Railscasts on has_many through and the Rails docs.

[http://guides.rubyonrails.org/association_basics.html#the-
ha...](http://guides.rubyonrails.org/association_basics.html#the-has_many-
through-association)

has_many :through handles the minority of complex data relationships, but it
gets used a lot and is a great starting place and can probably be helpful
immediately in your projects/tasks/comments app

~~~
rman666
Thank you joshcrews. I will look into this right away. Sounds useful.

------
jhuckestein
This might be overkill for you, but at a certain point I just set down for a
few days and read The Rails 3 Way front to cover. It's a reference of sorts
and it's very complete (afaik). I marked all the things I hadn't known or
thought was interesting and made a point of using then right away. It was
really dry at times but turned out to be well worth it.

[http://www.amazon.com/Rails-Edition-Addison-Wesley-
Professio...](http://www.amazon.com/Rails-Edition-Addison-Wesley-Professional-
Series/dp/0321601661)

------
Sathi
The following links might be of help:

<http://railscasts.com/episodes/355-hacking-with-arel>

<http://railscasts.com/episodes/354-squeel>

[http://railscasts.com/episodes/215-advanced-queries-in-
rails...](http://railscasts.com/episodes/215-advanced-queries-in-rails-3)

------
rman666
UPDATE from RMAN666 (who posted this question): Thank you all for the
suggestions and comments. I've already learned a lot from them and it's only
been two hours! I need to learn more about RDBMS and Rails' association
commands. The links you provided will really help. I now realize this is
probably more of an intermediate topic than an advanced topic :-)

------
Toshio
Learn SQL beyond simple statements. For instance, did you know that you can
write this in SQL: "SELECT * FROM marketing_promotions WHERE 201205 BETWEEN
month_from AND month_to" ? There are a lot of interesting things you can do
with SQL which Rails isn't necessarily provide a good API into. For instance,
did you know that you can create a custom crafted join view at the database
level, with a sophisticated where clause, then instruct ActiveRecord to treat
it as a real table? (This by the way sounds like what you need to do for your
particular problem.) Also have a look into SQL subqueries, they are a very
powerful tool.

~~~
rman666
Thank you Toshio!

