Self-regulatory models in Rails 3

I have an Entity model and want to display connections between Entities. those. Entity 1 is connected to Entity 2.

My thinking now is to create a connection model between two called Connection and make it work like a traditional rails connection table. Except for the columns: entity_one_id and entity_two_id, then set the many-to-many relationship between Entity and Connection.

This seems like a really non-elegant way to do this. I was wondering if anyone has any better ideas? Maybe something more rail that I just don’t see?

+5
source share
2 answers

. , , .

Rails Bates 'Railscast . , , , , .

+8

:

class User < ActiveRecord::Base
  has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
  has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
  has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
  has_many :friendships, :dependent => :destroy
end


class Friendship < ActiveRecord::Base
   belongs_to :user
   belongs_to :friend, :class_name => "User"
end
+1

All Articles