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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:05:17+00:00 2026-05-14T22:05:17+00:00

In my Rails application there is a model that has some has_one associations (this

  • 0

In my Rails application there is a model that has some has_one associations (this is a fabricated example):

class Person::Admin < ActiveRecord::Base
  has_one :person_monthly_revenue
  has_one :dude_monthly_niceness
  accepts_nested_attributes_for :person_monthly_revenue, :dude_monthly_niceness
end

class Person::MonthlyRevenue < ActiveRecord::Base
  belongs_to :person_admin
end

class Dude::MonthlyNiceness < ActiveRecord::Base
  belongs_to :person_admin
end

The application talks to a backend that computes some data and returns a piece of JSON like this:

{
    "dude_monthly_niceness": {
        "february": 1.1153232569518972, 
        "october": 1.1250217200558268, 
        "march": 1.3965786869658541, 
        "august": 1.6293418014601631, 
        "september": 1.4062771500697835, 
        "may": 1.7166279693955291, 
        "january": 1.0086401628086725, 
        "june": 1.5711510228365859, 
        "april": 1.5614525597326563, 
        "december": 0.99894169970474289, 
        "july": 1.7263264324994585, 
        "november": 0.95044938418509506
    }, 
    "person_monthly_revenue": {
        "february": 10.585596551505297, 
        "october": 10.574823016656749, 
        "march": 9.9125274764852787, 
        "august": 9.2111604702328922, 
        "september": 9.7905249446675153, 
        "may": 9.1329712474607962, 
        "january": 10.479614016604238, 
        "june": 9.3710235926961936, 
        "april": 9.5897372624830304, 
        "december": 10.052587677671438, 
        "july": 8.9508877843925561, 
        "november": 10.925339756096172
    }, 
}

To deserialize it, I use ActiveRecord’s from_json, but instead of a Person::Admin object with all the associations in place, I get this error:

>> Person::Admin.new.from_json(json)
NameError: uninitialized constant Person::Admin::DudeMonthlyNiceness

Am I doing something wrong?

Is there a better way to deserialize data? (I can modify the backend easily)

UPDATE: the original title was “How to deserialize from json to ActiveRecord objects with associations?” but it ended up being my mistake in specifying associations so I changed the title.

  • 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-14T22:05:17+00:00Added an answer on May 14, 2026 at 10:05 pm

    Your classes are in different namespaces. ActiveRecord transforms dude_monthly_niceness into DudeMonthlyNiceness class inside current namespace, which gives Person::Admin::DudeMonthlyNiceness, which doesn’t exist.

    So you should specify class names explicitly for associations (Do not know what exactly foreign keys you are using, so change if needed):

    class Person::Admin < ActiveRecord::Base
      has_one :person_monthly_revenue, :class_name => "Person::MonthlyRevenue", :foreign_key => "person_admin_id"
      has_one :dude_monthly_niceness, :class_name => "Dude::MonthlyNiceness", :foreign_key => "person_admin_id"
      accepts_nested_attributes_for :person_monthly_revenue, :dude_monthly_niceness
    end
    

    Another thing is that to be able to receive associated objects using accepts_nested_attributes_for, you need to end your json objects with _attributes:

    {
        "dude_monthly_niceness_attributes": {
            "february": 1.1153232569518972, 
            ...
            "november": 0.95044938418509506
        }, 
        "person_monthly_revenue_attributes": {
            "february": 10.585596551505297, 
            ...
            "november": 10.925339756096172
        }, 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails simple application that has two main models. A person model
I have a rails application that has three different types of users and I
Rails noob here. I have a rails application with (in this example) three tables.
I'm refactoring a Rails-based event registration application that has a checkout process that hits
Our Rails 3 application has models Person and Message . Messages can be specific
I have an rails 3 application where there are multiple registrations (diagnosis, patient, laboratory
If there's an exception in a Rails application, one gets an error page with
Is there an equivalent of htmlspecialchars_decode (PHP) in Ruby (for a Ruby-on-Rails application) Or
in my rails 3 application, there is a select_tag with an onchange event, which
My Rails application has a number of tasks which are offloaded into background processes,

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.