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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:18:36+00:00 2026-06-11T17:18:36+00:00

I have a tutorials model that looks like this: class Tutorial < ActiveRecord::Base attr_accessible

  • 0

I have a tutorials model that looks like this:

class Tutorial < ActiveRecord::Base
  attr_accessible :content, :title
  has_many :tutorial_categories
end

and a tutorial_category model that looks like this:

class TutorialCategory < ActiveRecord::Base
  attr_accessible :name, :tutorial_id
  belongs_to :tutorial
end

When I know the tutorial_category via something like @tutorial_category = TutorialCategory.find(params[:id])

How can I get all the tutorials that belong to that tutorial category. Put another way, once I know @tutorial_category.name, I want all tutorials that have the same tutorial_category.name set.

Do I need to use a join or include to do this?

  • 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-11T17:18:37+00:00Added an answer on June 11, 2026 at 5:18 pm

    First you’re using the wrong type of relationship here. You’re trying to model a many to many relationship with a many to one setup. I’d setup your models like the following. This assumes you have a join table called categories_tutorials with the fields category_id and tutorial_id. Also note I changed the name of your categories model as it was very close to the conventional Rails join table syntax.

    class Tutorial < ActiveRecord::Base
      attr_accessible :content, :title
      has_and_belongs_to_many :categories
    end
    
    class Category < ActiveRecord::Base
      attr_accessible :name
      has_and_belongs_to_many :tutorials
    end
    

    To find all the Tutorials for a given category you can now do something like the following:

    a = Category.find(:params[:id])
    tutorials = a.tutorials
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: class Pdf extends \lithium\data\Model { protected
Having followed a tutorial online, I have a sessions controller that looks like: class
I have the following listview : http://www.vogella.de/articles/AndroidListView/article.html the chaper: 8. Tutorial: Domain Model and
This one is a long one, bellow is my primary class that holds the
I'm following this tutorial which is working splendidly for has_many :through relationships. I've got
I have a question regarding many-to-many relationships, specifically has_many :through associations. All the tutorials
I am creating a HelloWorld web/spring application from scratch. I have followed this tutorial
I have a model, below, and I would like to get all the distinct
I have a group of model peer classes that should all have the same
I'm following this tutorial http://codemagento.com/2011/03/creating-custom-magento-reports/ creating the simple report module. I have all the

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.