Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:30:40+00:00 2026-06-13T23:30:40+00:00

I have following subscription creating system, right now when I`m selection available subscription groups(Marketing,

  • 0

I have following subscription creating system, right now when I`m selection available subscription groups(Marketing, Sales) action Save Subscription create this two subscriptions:

@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 

So I can only add new Subscriptions(in this particular example i can only add Engineering)

Subscriptions

How to refactor that action to be able also destroy subscription groups by uncheck them(ex. I want to unsubscribe from Marketing group) ?

So when I choose Marketing and Enginnering then params[:subscription][:app_id] will be equal [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 %>

Relations:

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

# == 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

So the issue is how to find which apps has been unchecked?

Then remove subscription for them and remove Feeds using Feed.app_destroy_items(app)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:30:41+00:00Added an answer on June 13, 2026 at 11:30 pm

    Ok, so in your case Susbcription is a join model between App and User. That means you can see a User’s apps like so:

    user.apps # returns array of apps
    

    That means you can also set them in the same fashion. So something like this should work:

    if params[:subscription] and params[:subscription][:app_ids] #call it app_ids since you're getting an array of them.                   
      apps = App.find(params[:subscription][:app_ids])
      current_user.apps = apps
    else
      current_user.apps = []
    end
    current_user.save
    

    Because subscription is a join model and you’ve got it linked on both ends, you don’t really need to bother loading the model directly in most cases.


    Updated above to show handling unchecking all apps.

    Responding to comments:

    If you need to know the difference between the old and new apps, you could do something like:

    original_apps = current_user.apps
    
    ... the code from above ...
    
    deleted_apps = original_apps - current_user.apps
    deleted_apps.each do |app|
    
      ... whatever ...
    
    end
    

    However, it looks to me like your controller is getting stupid fat here. Why isn’t more of this being handled at the model layer?

    For instance, for Feed.app_destroy_items(app), why not have a callback in subscription for after destroy?

    after_destroy :destroy_app_from_feed
    def destroy_app_from_feed
      Feed.app_destroy_items(app)
    end
    

    As for setting approved=true … how would a user get a subscription that is not approved? Think about it that way. They don’t get the option to click on ones they aren’t shown, right? Is this an issue where they have to pay to get certain things?

    Pretty much deciding whether or not a user is able to subscribe to something should not be happening at the controller level. So, I would say put a callback in subscription that won’t allow it to be saved for a user who isn’t authorized, then you can respond if the user saves or not, and if not you show the user errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following action to confirm a PIN code. def create @confirm =
I have the following: class User < ActiveRecord::Base has_one :subscription end class Subscription <
I have the following model. Subscription Packages PackageWidgets Widgets ------------ -------- -------------- ------- ID
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have produced a result from the following query as shown below.Now the result
I have the following code: using (var db = new IntDB()) { var subscription
Hi I have three models: company, plan and subscription with following association class Company
I have an ASP.NET MVC web app which has a really basic subscription system
I got a minor problem. To start, I have the following tables: 'groups', 'pages'
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.