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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:47:48+00:00 2026-05-23T04:47:48+00:00

Hello fellow developers! Recently I’ve been playing with Rails 3.0 and after quite a

  • 0

Hello fellow developers!
Recently I’ve been playing with Rails 3.0 and after quite a bit of research I’m kinda stuck.
I want to know what approach or solution is the best in my case(I couldn’t find an answer yet).
So what I’m trying to achieve is simple and straight forward.

I want to do something like this:

class User < ActiveRecord::Base
    has_many :feeds
    has_many :casts, :through => :feeds
end

class Feed < ActiveRecord::Base
  has_many :users
  has_many :casts
end

class Cast < ActiveRecord::Base
  belongs_to :feed
end

So at the end I need to have methods like User.first.feeds to get all the user’s feeds and User.first.casts to get all the user’s casts through his/her feeds. Also would be nice to have Feed.first.casts and Feed.first.users. Pretty simple, right, but I’m also having a hard time to create migrations for what I’m trying to achieve.

I know that the code above won’t work – I’ve been playing with it so this is just the concept of what I’m trying to achieve.

Basically my questions are: should I do it through join model somehow or use scopes?(also could you give a code snippet) and how do I do migration for that?

Thanks, and sorry I couldn’t find much information on the web regarding this simple case.

Edit: has_and_belongs_to_many on User and Feed won’t work in my case because it won’t allow me to have @user.casts, it gives only @user.feeds and @feed.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-23T04:47:49+00:00Added an answer on May 23, 2026 at 4:47 am

    What you want is a many to many relationship between User and Feed.

    You’ll want something like this in your code for the User and Feed relationship to work.

    class User < ActiveRecord::Base
      has_and_belongs_to_many :feeds
    end
    
    class Feed < ActiveRecord::Base
      has_and_belongs_to_many :users
    end
    

    You can read more about this in the Rails Guides – http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

    You may also want to look at using has_many :through with an intermediate model for this (explained here http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many) if you’d like to store any meta data for a user-feed relationship record.

    Edit: I managed to get a similar setup working on 3.0 as well as 3.1 (using has_many :through).

    Here are the contents of my models.

    ➜  app  cat app/models/*
    class Cast < ActiveRecord::Base
      belongs_to :feed
    end
    
    class Feed < ActiveRecord::Base
      has_many :subscriptions
      has_many :casts
    end
    
    class Subscription < ActiveRecord::Base
      belongs_to :user
      belongs_to :feed
    end
    
    class User < ActiveRecord::Base
      has_many :subscriptions
      has_many :feeds, :through => :subscriptions
    
      # For 3.1
      has_many :casts, :through => :feeds
      # For 3.0
      def casts
        Cast.joins(:feed => :subscriptions).where("subscriptions.user_id" => self.id)
      end
    end
    

    and here are the migrations I used

    ➜  app  cat db/migrate/*
    class CreateUsers < ActiveRecord::Migration
      def self.up
        create_table :users do |t|
          t.string :name
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :users
      end
    end
    class CreateFeeds < ActiveRecord::Migration
      def self.up
        create_table :feeds do |t|
          t.string :name
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :feeds
      end
    end
    class CreateCasts < ActiveRecord::Migration
      def self.up
        create_table :casts do |t|
          t.string :name
          t.integer :feed_id
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :casts
      end
    end
    class CreateSubscriptions < ActiveRecord::Migration
      def self.up
        create_table :subscriptions do |t|
          t.integer :feed_id
          t.integer :user_id
        end
      end
    
      def self.down
        drop_table :subscriptions
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello fellow java developers. I'm having a bit of an issue here. I have
Hello fellow software developers. I want to distribute a C program which is scriptable
Hello fellow developers... just to make sure, I want to ask this question: How
Hello fellow developers. First of all I apologize beforehand for the wall of text
Hello fellow developers. I have previously made a facebook fan page with an iframe
Hello fellow Grails Developers! I was wondering if you could help me with what
hello fellow java developers. I'm having a very strange issue. I'm trying to read
Hello fellow developers, I am trying to achieve something in Android and I would
Hello fellow developers, I am looking for a solution regarging Facebook Connect. We developed
Hello fellow programmers! Recently, I started learning Git, and pretty quickly discovered the amazing

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.