次のサブスクリプション作成システムがあります。現在、利用可能なサブスクリプショングループ(マーケティング、販売)を選択するとSave Subscription
、次の2つのサブスクリプションが作成されます。
@apps = App.all
if request.post?
if params[:subscription] and params[:subscription][:app_id]
params[:subscription][:app_id].each do |app_id|
Subscription.create_unique({user_id: current_user.id, app_id: app_id, approved: true})
end
redirect_to root_path
end
end
@subscriptions = current_user.subscriptions
したがって、新しいものだけを追加できますSubscriptions
(この特定の例では、追加することしかできませんEngineering
)
そのアクションをリファクタリングして、サブスクリプショングループのチェックを外して、サブスクリプショングループも破棄できるようにするにはどうすればよいですか(例:マーケティンググループのサブスクリプションを解除したい)?
だから私がマーケティングとエンジニアリングを選ぶとき、それparams[:subscription][:app_id]
は等しくなります[marketing.id, engineering.id]
# app_menu.html.erb
<%= form_for :subscription do |f| %> # this form goes to app_menu action above
<ul>
<% @apps.each do |app| %>
<li>
<%= check_box_tag app.id, current_user.access?(app) %><span><%= app.name %></span>
</li>
<% end %>
</ul>
<%= f.submit %>
<% end %>
<% end %>
関係:
App
has_many :subscriptions
has_many :users, through: :subscriptions
User
belongs_to :app
has_many :subscriptions, :dependent => :destroy
Subscription
belongs_to :user
belongs_to :app
def self.create_unique(p)
s = Subscription.find :first, :conditions => ['user_id = ? AND app_id = ?', p[:user_id], p[:app_id]]
Subscription.create(p) if !s
end
スキーマ
# == Schema Information
#
# Table name: subscriptions
#
# admin :boolean
# app_id :integer
# created_at :datetime
# id :integer not null, primary key
# updated_at :datetime
# user_id :integer
#
# Table name: apps
#
# created_at :datetime
# id :integer not null, primary key
# name :string(255)
# updated_at :datetime
# user_id :integer
#
# Table name: users
#
# app_id :integer
# created_at :datetime
# id :integer not null, primary key
# updated_at :datetime
では、問題は、どのアプリがチェックされていないかを見つける方法です。
次に、それらのサブスクリプションを削除し、を使用してフィードを削除しますFeed.app_destroy_items(app)