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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:34:43+00:00 2026-05-28T03:34:43+00:00

I have set up a twitter-like following model. Users can all subscribe to each

  • 0

I have set up a twitter-like following model. Users can all subscribe to each other. I am getting an error in my users controller when trying to create the relationship.

user.rb:

has_many :subscriptions
has_many :providers, :through => :subscriptions

has_many :followings, :class_name => "Subscription"
has_many :followers, :through => :followings

subscription.rb

belongs_to :provider, :class_name => 'User', :foreign_key => "provider_id"
belongs_to :follower, :class_name => 'User', :foreign_key => "follower_id"

users_controller.rb

69 def follow
70   logger.debug params.to_yaml
71   @user = User.find(params["user_id"])
72   logger.debug @user.to_yaml
73   if current_user.providers << @user
74     flash[:notice] = "Subscribed"
75   else
76     flash[:error] = "Unable to subscribe."
77   end
78 end

This is the error when I call follow:

ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
  app/controllers/users_controller.rb:73:in `follow'

I have verified that I ran rake db:migrate – the subscription table has two fields provider_id and follower_id. Can anyone help me with the error and explain why it is looking for a ‘user_id’ attribute?

Update:

show.html.erb:

<%= button_to "Subscribe", user_follow_path(@user), :remote => true %>

routes.rb:

resources :users do
  resources :locations
  resources :saved_events
  resources :saved_locations
  post "follow"
end

rake routes | grep follow:

user_follow POST     /users/:user_id/follow(.:format)                    {:action=>"follow", :controller=>"users"}
  • 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-28T03:34:44+00:00Added an answer on May 28, 2026 at 3:34 am

    Using Michael Hartl’s tutorial as a guide, I came up with this solution, which fixes the data model so that collection functions work as they should.

    Make the provider id accessible and remove the foreign keys in the subscription model.

    subscription.rb:

    attr_accessible :provider_id
    belongs_to :provider, :class_name => 'User'
    belongs_to :follower, :class_name => 'User'
    

    Add foreign keys for subscriptions and reverse_subscriptions in the user model.

    user.rb:

    has_many :subscriptions, :foreign_key => "subscriber_id", :dependent => :destroy
    has_many :subscribed_to, :through => :subscriptions, :source => :provider
    
    has_many :reverse_subscriptions, :class_name => "Subscription", :foreign_key => "provider_id", :dependent => :destroy
    has_many :followers, :through => :reverse_subscriptions
    

    I also added helper methods to the user model.

    user.rb:

    def following?(provider)
      subscriptions.find_by_provider_id(provider)
    end
    
    def follow!(provider)
      subscriptions.create!(:provider_id => provider.id)
    end
    
    def unfollow!(provider)
      subscriptions.find_by_provider_id(provider).destroy
    end
    

    Then, in the controller, we can call follow! or unfollow!

    user_controller.rb:

    ...
    current_user.unfollow!(@user)
    ...
    current_user.follow!(@user)
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up multiple targets in a single xml file. I expect all
I have set up Team Foundation Server - Team Web Access SP1. I can
I have set up a grid and bound it to a collection. I can
I have set up a Core Data model where I have two objects, say
I have successfully set up my twitter callback on my webpage and everything is
I have a array that looks like this: $sites = array('Twitter' => 'http://twitter.com/home?status=$status', 'Digg'
I have successfully set up a friendship self referencial association for users in my
I'm trying to set up a PowerShell profile so all of my machines have
I have the following code that converts my twitter account rss feed into a
Considering the set of Twitter users nodes and the relation u follows v as

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.