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 7779745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:42:59+00:00 2026-06-01T18:42:59+00:00

I can’t get my subscription to update with my product and also duplicate the

  • 0

I can’t get my subscription to update with my product and also duplicate the attributes of the product to the same fields as my subscription table.

My associations are, subscriptions and products belong to a User but a Product has many subscriptions.

Subscription.rb

class Subscription
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true
end

Product.rb

class Product
  belongs_to :user
  has_many :subscriptions, :as => :subscribable, :dependent => :destroy
end

User.rb

class User
  has_many :products, :dependent => :destroy
  has_many :subscriptions, :foreign_key => :subscriber_id, :dependent => :destroy
end

Then the Product and Subscription table with the same columns that I’m trying to duplicate:

create_table :products do |t|
   t.string  :name
   t.decimal :price
   t.integer :user_id
end

create_table :subscriptions do |t|
   t.string  :name
   t.decimal :price
   t.integer :subscriber_id # same as user_id
   t.integer :subscribable_id
   t.string  :subscribable_type
end

ProductsController

def edit
   @product = Product.find(params[:id])
end

def update
   @product = Product.find(params[:id])
   if @product.update_attributes(params[:product])
      redirect_to(@product, :notice => 'Successfully Updated.')   
   else 
      render :back
   end
end

ProductObserver

class ProductObserver < ActiveRecord::Observer
  def after_update(product)
    if self.subscriptions.find_by_subscribable_id_and_subscribable_type(subscribable_id, subscribable_type)
        subscription = Subscription.find_by_subscribable_id_and_subscribable_type(subscribable_id, subscribable_type)
        self.subscription.update_attributes(params[:subscription]).select{ |key, _| Subscription.attribute_names.include? key })
    end
  end
end

What the after_update is suppose to do is:

  1. Check if a subscription for the particular product exist and if it does….
  2. Update the current users subscription with the products new edited attributes.

Right now, the Subscription doesn’t update when the Product does. What do I need to fix about this code to get it to do this? What about when duplicating the product fields to its subscription?

  • 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-01T18:43:00+00:00Added an answer on June 1, 2026 at 6:43 pm

    Not sure if that was just a typo, but your observer is wrong. self is not your product in an observer. Instead you should use product (the given parameter) instead.

    Secondly your lookup of the subscriptions seems wrong too. You are using subscribable_id and subscribable_type which are not defined and thus just nil. I think you want to use product.id and 'Product', but then you could just iterate over all subscriptions of the product. product.subscriptions returns all subscriptions linked to that product.

    Lastly, if you are going to keep price and name of a subscription always in sync with the linked product, why do not something like this instead:

     create_table :products do |t|
       t.string  :name
       t.decimal :price
       t.integer :user_id
    end
    
    create_table :subscriptions do |t|
       t.integer :subscriber_id # same as user_id
       t.integer :subscribable_id
       t.string  :subscribable_type
    end
    

    and inside your subscription model do

    class Subscription
      belongs_to :subscriber, :class_name => "User"
      belongs_to :subscribable, :polymorphic => true
    
      delegate :name, :price, :to => :subscribable, :allow_nil => true
    end
    

    Hope this helps.

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

Sidebar

Related Questions

Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can i get the source code for a WAMP stack installer somewhere? Any help
Can we change the HTML or its attributes of the selected part of the
Can find why i get this error can someone help? package Android.data; public class
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say
Can I authenticate with just Google account username and password instead of using OAuth?
Can we change the default action of the edit selected row button? Here is
Can someone thoroughly explain the last line of the following code: def myMethod(self): #

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.