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

  • Home
  • SEARCH
  • 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 322243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:55:15+00:00 2026-05-12T08:55:15+00:00

I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription

  • 0

I have the following models:

class User < ActiveRecord::Base
  has_many :subscriptions
end

class Subscription < ActiveRecord::Base
  belongs_to :user
  belongs_to :queue
end

class Queue < ActiveRecord::Base
  has_many :subscriptions
end

I want to have some meta-data in the Subscription class and allow users to maintain the details of each of their subscriptions with each subscriptions meta-data. Queues produce messages, and these will be sent to users who have Subscriptions to the Queue.

As I see it the resource I want to have is a list of subscriptions, ie the user will fill in a form that has all the Queues they can subscribe to and set some metadata for each one. How can I create a RESTful Rails resource to achieve this? Have I designed my Subscription class wrong?

I presently have this in my routes.rb:

map.resources :users do |user|
  user.resources :subscriptions
end

But this makes each subscription a resource and not the list of subscriptions a single resource.

Thanks.

  • 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-05-12T08:55:15+00:00Added an answer on May 12, 2026 at 8:55 am

    This can be done quite easily using accepts_nested_attributes_for and fields_for:

    First in the User model you do the following:

    class User < ActiveRecord::Base
      has_many :subscriptions
      accepts_nested_attributes_for :subscriptions, :reject_if => proc { |attributes| attributes['queue_id'].to_i.zero? }
    
      # if you hit scaling issues, optimized the following two methods
      # at the moment this code is suffering from the N+1 problem
      def subscription_for(queue)
        subscriptions.find_or_initialize_by_queue_id queue.id
      end
    
      def subscribed_to?(queue)
        subscriptions.find_by_queue_id queue.id
      end
    
    end
    

    That will allow you to create and update child records using the subscriptions_attributes setter. For more details on the possibilities see accepts_nested_attributes_for

    Now you need to set up the routes and controller to do the following:

    map.resources :users do |user|
      user.resource :subscriptions  # notice the singular resource
    end
    
    class SubscriptionsController < ActionController::Base
    
      def edit
        @user = User.find params[:user_id]
      end
    
      def update
        @user = User.find params[:user_id]
        if @user.update_attributes(params[:user])
          flash[:notice] = "updated subscriptions"
          redirect_to account_path
        else
          render :action => "edit"
        end
      end
    
    end
    

    So far this is bog standard, the magic happens in the views and how you set up the params:
    app/views/subscriptions/edit.html.erb

    <% form_for @user, :url => user_subscription_path(@user), :method => :put do |f| %>
      <% for queue in @queues %>
        <% f.fields_for "subscriptions[]", @user.subscription_for(queue) do |sf| %>
          <div>
            <%= sf.check_box :queue_id, :value => queue.id, :checked => @user.subscribed_to?(queue) %>
            <%= queue.name %>
            <%= sf.text_field :random_other_data %>
          </div>
        <% end %>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Once you have the MethodInfo via Reflection, you can use… May 12, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer There isn't a 1 step answer to this but you… May 12, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer New answer: In HTML5 you can add the multiple attribute… May 12, 2026 at 6:17 pm

Related Questions

I'm building a small twitter style microblogging service where users can follow other users
I cannot seem to get a nested form to generate in a rails view
This is a continuation of this question: Original Question (SO) The answer to this
Suppose you have two models, User and City, joined by a third model CityPermission:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.