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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:38:25+00:00 2026-06-15T03:38:25+00:00

I am converting a User object to json via: user.to_json :methods => :new_cookies the

  • 0

I am converting a User object to json via:

user.to_json :methods => :new_cookies

the new_cookies method is:

cookies.all :include => :fortune, :conditions => {:opened => false}

This embed the cookies inside the user json object, but I want fortune to be embedded inside the cookie object as well. I passed inside :include => :fortune but that doesn’t that work.

Is this possible?

Models:

class User < ActiveRecord::Base
    has_many :cookies
    has_many :fortunes, :through => :cookies

    def new_cookies
        cookies.all :include => :fortune, :conditions => {:opened => false}
    end
end

class Cookie < ActiveRecord::Base
    belongs_to :user
    belongs_to :fortune
end

class Fortune < ActiveRecord::Base
    serialize :rstatuses
    serialize :genders 

    has_many :cookies
    has_many :users, :through => :cookies
end
  • 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-15T03:38:28+00:00Added an answer on June 15, 2026 at 3:38 am

    I am not sure that the :includes => :fortune option works as you expect (or perhaps at all) — near the end of this section of the current Rails guides it mentions this option for finder methods other than .all.

    I assume it works similarly to the new Active Relation query interface, e.g. Cookies.include(:fortunes).where(:opened => false) — in this case, Rails “eager loads” the related records, meaning fortunes are fetched as part of the query for cookies. This is a performance enhancement, but doesn’t otherwise change the behavior of Rails.

    As I noted in the comments, I think as_json will do what you want — it defines what is and is not part of the object when serialized using to_json. You specify methods that should be called in addition to (or to exclude) the methods of the data-backed object itself, for example, your new_cookies method.

    In this example, I have added as_json to User (which gets Cookie) and also to Cookie in hopes that each cookie will nest its fortunes in its JSON. See below for an alternative.

    class User < ActiveRecord::Base
      has_many :cookies
      has_many :fortunes, :through => :cookies
    
      def as_json(option = {})
        super(:methods => :new_cookies)
      end
    
      def new_cookies
        cookies.all :conditions => {:opened => false}
      end
    end
    
    class Cookie < ActiveRecord::Base
      belongs_to :user
      belongs_to :fortune
    
      def as_json(options = {})
        super(:methods => :cookie_fortune)  # or perhaps just :fortune 
      end
    
      def cookie_fortune
        self.fortune
      end
    end
    

    In a case where I was writing an API and didn’t need to reflect the nested relationships between objects in the JSON, in the controller, I used something like

    respond_to do |format|
      format.html 
      format.json { render json: { :foo => @foo, :bar => @bar }}
    end
    

    to produce parallel nodes (objects) in the JSON.

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

Sidebar

Related Questions

What's wrong here? res.render('/somepage', {user:req.session.user}) It leads to Converting circular structure to JSON errors,
I am struggling with converting multi-rows values which are belong to the same user
Converting json to oher type should be easy. From the Play! documentation : var
After converting project to ARC, The converting operation removed all the - assign, retain
When converting a project (in which a template method of IComparable was used a
How can I go about storing a vb.net user defined object in a sql
I have a list and i am adding a value using add(int,Object) method but
The following function connects to an API and returns a json object. Although it
Possible Duplicate: Converting Raw HTTP Request into HTTPWebRequest Object I've got a custom HTTP
I'm saving user profiles in DynamoDB, and using attributes for converting the .net types

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.