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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:44:05+00:00 2026-05-20T19:44:05+00:00

Hi I got a model Category and another model Products. The have the relationship

  • 0

Hi
I got a model Category and another model Products. The have the relationship – has_and_belongs_to_many.

When I open the console and type Category.first.products, I receive a list with the products attached to that Category.

But when I try to generate an xml file with the show model I get:

undefined method `type' for nil:NilClass

I’ve tested the code below on other models I’ve made that has_many relationships and it works like how I want it to work. But won’t work with this relationship, maybe it doesn’t have anything to with that?

  def show

@categories = Category.find(params[:id])

@products = @categories.products

respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @products }
  format.json { render :json => @products }
end

end

I’m new to this as you can see…

  • 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-20T19:44:06+00:00Added an answer on May 20, 2026 at 7:44 pm

    Thanks for your replies, I continued investigating and experimented with the controllers, and just tried with. @category.products.all, and it worked. The sad thing is that I don’t know why it did… Especially when it works for me in the console… But I guess I’m happy to have solved the problem in one way so to speak…

    def show
    @category = Category.find(params[:id])
    
    @products = @category.products.all
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml { render :xml => @products }
    end
    

    end

    But I will use the log and the terminal logger when I debug more than before! Thanks for the tips!

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

Sidebar

Related Questions

In our Core domain model design, we have got a class called Category whose
I have got a category - model which i am using it for my
I've got two models Category and Item. Category can have many items and item
Suppose I have got a model ArticleVersion in my project which is defined as:
I got a Event model that HABTM Categories. The relationship works fine and I
Ive got a simple Model called Category. The model is has three fields :
I got 2 models class Category(models.Model): name = models.CharField(max_length=30, unique=True) class Post(models.Model): ...... category
I got a problem with Entity Framework 4.0 I have a hierarchical table Category:
I'm trying to map a fairly standard category model using EF Code First public
I've got some categories in Magento which have got Static block and products 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.