我有以下订阅创建系统,现在当我选择可用订阅组(营销、销售)操作时Save Subscription
创建这两个订阅:
@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)