Ask HN: How can I learn "advanced" data modelling for Rails?
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.
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.
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?
23 comments
[ 7.0 ms ] story [ 75.8 ms ] threadMight 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.
http://railscasts.com/episodes/355-hacking-with-arel
http://railscasts.com/episodes/354-squeel
http://railscasts.com/episodes/215-advanced-queries-in-rails...
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...
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.
http://sql.learncodethehardway.org/book/ would be a good start.
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
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;
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...
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
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!
http://www.amazon.com/Rails-Edition-Addison-Wesley-Professio...