HABTM - , , .
create_table :subscriptions do |t|
t.column :author_id, :integer
t.column :subscriber_id, :integer
end
:
class Author < ActiveRecord::Base
has_and_belongs_to_many :subscribers
:class_name => "Author",
:join_table => "subscriptions",
:association_foreign_key => "subscriber_id"
def subscriptions
self.subscribers.find(:all, :subscriber_id=>author.id)
end
end
:
def subscribed_to_author
subscribers
end
def subscribed_by_author(author)
self.subscribers.find(:all, :subscriber_id=>author.id)
end
( SubscriptionsController RESTy)
SubscriptionsController < ApplicationController
def create
@author = Author.find(params[:author_id]
@user = current_user
@author.subscribers << @user
@author.save
end
end
..
@author.subscribers.each do |s|
s.name
end
<%= render :partial => @author.subscribers -%>
<%= render :partial => @author.subscriptions -%>