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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:31:41+00:00 2026-05-27T13:31:41+00:00

I am basically looking for a way to implement a polymorphic model Subscription in

  • 0

I am basically looking for a way to implement a polymorphic model Subscription in my app right now

There are entries, users and subscriptions in my app right now. A user can subscribe to an entry. Later on, a user can subscribe to something else. Hence polymorphic.

I have been following the rails cast on polymorphic model. However, I have made some modifications:

  # Param: {"subscribable_type": "entry|something", "subscribable_id": int}
  def create
    @subscribable = find_subscribable(params[:subscription])
    @subscription = @subscribable.subscriptions.build(params[:subscription])
    @subscription.user_id = current_user.id
    if @subscription.save
      redirect_to :back, :notice => "Successfully created subscription."
    else
      redirect_to :back, :notice => "Failed creating subscription."
    end
  end

  def find_subscribable(instance_params)
    class_name = instance_params["subscribable_type"].classify.constantize
    class_name.find(instance_params["subscribable_id"])
  end

and in my models:

class Subscription < ActiveRecord::Base
  attr_accessible :user_id, :subscribable_id, :subscribable_type

  belongs_to :user

  belongs_to :subscribable, :polymorphic => true
end

class Entry < ActiveRecord::Base
  attr_accessible :title, :body, :img, :author_id

  belongs_to :author, :class_name => "User", :foreign_key => "author_id"
  has_many :tidbits
  has_and_belongs_to_many :tags

  has_many :subscriptions, :as => :subscribable
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

from the console:

1.9.2p290 :037 > Subscription.last
  Subscription Load (0.1ms)  SELECT "subscriptions".* FROM "subscriptions" ORDER BY "subscriptions"."id" DESC LIMIT 1
 => #<Subscription id: 3, user_id: 1, subscribable_id: 3, subscribable_type: "entry", created_at: "2011-12-15 07:17:53", updated_at: "2011-12-15 07:17:53"> 

Somehow, the entry above has no subscriptions:

1.9.2p290 :040 > Entry.find(3).subscriptions
  Entry Load (0.2ms)  SELECT "entries".* FROM "entries" WHERE "entries"."id" = ? LIMIT 1  [["id", 3]]
  Subscription Load (0.2ms)  SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."subscribable_id" = 3 AND "subscriptions"."subscribable_type" = 'Entry'
 => [] 

What am I doing wrong?

And how should I set up User so that I can access all the user’s entries subscriptions and other forms of subscriptions?

  • 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-27T13:31:42+00:00Added an answer on May 27, 2026 at 1:31 pm

    iThe second query is looking for subscribable_type = 'Entry' (upper case), but the output from the first query shows that the persisted data has subscribable_type = 'entry' (lower case).

    You need to change whatever us calling the create method such that it passes uppercase Entry.

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

Sidebar

Related Questions

Still the newbie in Scala and I'm now looking for a way to implement
I'm basically looking for a way to automate typing stuff like the following: cout
Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or
I'm looking for a way of instrumenting a binary with my own functions. Basically,
I am basically looking for a prettier way to write: do_something() if my_string ==
I'm looking for the best way to implement the following. I have a MySQL
So right now i need to create and implement an extension of the Python
I'm looking for suggestions on the best way to implement a full-text search on
I'm basically looking for a way to access a hashtable value using a two-dimensional
My team's current project involves re-writing retrieval libraries in JavaScript. We are basically looking

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.