Rails double-sided self-referencing has_many: through - controller configuration?

In my Rails application, a user can subscribe to other users. You need to be able to receive subscription subscriptions and subscribers from a user instance.

Now I have a model Userand a model Subscription. Subscriptionis a model of unification (unification Userin User) in a two-way self-referential communication has_many :through.

Subscription:

class Subscription < ActiveRecord::Base
  belongs_to :from_user, :class_name => 'User'
  belongs_to :to_user, :class_name => 'User'
end

User:

class User < ActiveRecord::Base
  ...
  has_many :subscriptions_as_subscriber, :foreign_key => 'from_user_id', :class_name => 'Subscription', :dependent => :destroy
  has_many :subscriptions_as_subscription, :foreign_key => 'to_user_id', :class_name => 'Subscription', :dependent => :destroy
  has_many :subscribers, :through => :subscriptions_as_subscription, :source => :from_user
  has_many :subscriptions, :through => :subscriptions_as_subscriber, :source => :to_user
  ...
end

First question: is it better for me to service another installation?

The second question is: how can I create a controller to create and destroy signatures and access subscriptions and subscribers of users?

SubscriptionsController create destroy subscriptions subscribers UsersController. , , ( RESTful?) . index SubscriptionsController, ?

+3
1

awesome_nested_set collect_idea. , .

, : http://www.justinball.com/2009/01/18/heirarchies-trees-jquery-prototype-scriptaculous-and-acts_as_nested_set/

UPDATE:

:

Subscriber
 + subscription
   + subscription
     + subscription
   + subscription
   + subscription
 + subscription
+1

Source: https://habr.com/ru/post/1751320/


All Articles