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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:48:48+00:00 2026-05-15T12:48:48+00:00

I have a simple has_many/belongs_to relationship between Report and Chart. The issue I’m having

  • 0

I have a simple has_many/belongs_to relationship between Report and Chart. The issue I’m having is that my Chart model is a parent that has children.

So in my Report model I have

class Report < ActiveRecord::Base
  has_many :charts
end

And my Chart model is a parent, where Pie, Line, Bar all inherit from Chart. I’m not sure where the belongs_to :report belongs within the chart model, or children of chart model. I get errors when I attempt to access chart.report because the object is of type “Class”

undefined local variable or method `report’ for #< Class:0x104974b90>

The Chart model uses STI so its pulling say.. ‘Gender’ from the chart_type column in the charts table.. what am I missing?

EDIT

      Chart
      /  \
    Pie  Line
    / \
   /   \
Gender Sex

I am (using STI) instantiating an object of type Gender, or Sex. Hopefully this helps a bit more.

I have a feeling that its caused by

@chart.update_attributes(params[:chart])

because when submitted its actually params[:chart] its params[:gender] or params[:sex]

  • 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-15T12:48:48+00:00Added an answer on May 15, 2026 at 12:48 pm

    Your problem is that you have one controller receiving all these different types of models. When you use the Rails form helpers, they’re taking the object type from the instance and using that to populate the params. This means that you have params[:gender] instead of params[:chart]

    You can fix this by overriding the method that Rails uses to generate the form. Put the following code in your base chart class:

    def self.model_name
      name = "chart"
      name.instance_eval do
        def plural;   pluralize;   end
        def singular; singularize; end
      end
      return name
    end
    

    Now, any class that is a child of Chart will be passed into your receiving action as params[:chart]

    Keep in mind that changing the object name as I’ve outlined above could break functionality of some plugins/gems that rely on it. You should look into having multiple controllers. Instead of having a ChartsController to receive all of the data, have a GenderChartsController and a LineChartsController. This provides two benefits:

    1. You don’t need to change the object name
    2. If you need custom code for a specific type of chart in the future, you don’t have to add conditional statements to make it work, as the different controllers have segregated it for you.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple has_many and belongs_to relationship between two activerecords and when
I have a simple has_one/belongs_to relationship between two models. This is a new association
I have a model that has amenities with a 1:1 relationship: class Listing <
So I have a simple post model that has_many tags :through post_tags. This allows
I have a simple rails application were book belongs_to user and user has_many book.
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
I have a simple mapview that has the following viewdidload method and didupdate to
I have a simple web service that has an API third party developers are
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a one to many relationship between Admins and Users. Admin has_many :users

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.