Modelling Relationships as objects

DHH in Rails Confeerence pointed out that abstract relationships should
be
converted into concrete objects, CRUDying up relationships.
An example would be

class User < ActiveRecord::Base
has_many :subscriptions
has_many :news_categories, :through => :subscriptions
end

class NewsCategory < ActiveRecord::Base
has_many :subscriptions
has_many :users, :through => :subscriptions
end

class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :news_category
end

User and NewsCategory(2 tables in database) have m:n relation and hence
they
would require another db table to represent the
relationship.(User_NewsCategory); now they r 2 ways to model these
objects
and relations in Rails,

  1. Use habtm with 2 models for 2 db tables.
  2. 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
an
object.

This is fine with m:n relationships, but how about m:1/1:m and 1:1
relationship types?
Example, (Blog) 1:m (comments); In this case I have only 2 db tables
Blog
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?

Regards,
Jatinder

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs