DHH in Rails Confeerence pointed out that abstract relationships should
converted into concrete objects, CRUDying up relationships.
An example would be
class User < ActiveRecord::Base
has_many :news_categories, :through => :subscriptions
class NewsCategory < ActiveRecord::Base
has_many :users, :through => :subscriptions
class Subscription < ActiveRecord::Base
User and NewsCategory(2 tables in database) have m:n relation and hence
would require another db table to represent the
relationship.(User_NewsCategory); now they r 2 ways to model these
and relations in Rails,
- Use habtm with 2 models for 2 db tables.
- Use has_many :through with 3 models and 3 db tables.
2nd would be of choice if we want the relationship to be converted in to
This is fine with m:n relationships, but how about m:1/1:m and 1:1
Example, (Blog) 1:m (comments); In this case I have only 2 db tables
and Comments (no join table required…); if I use has_many/belongs_to
combination then there wont be a model for relationship.
How do I make the relationship concrete for m:1 and 1:1 relations?