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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:19:24+00:00 2026-06-15T02:19:24+00:00

I have models like this: class User < ActiveRecord::Base has_many :cookies has_many :fortunes, :through

  • 0

I have models like this:

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

    def self.find_by_shortened_id(shortened_id)
        find(shortened_id.alphadecimal)
    end

    def shortened_id
        self.id.alphadecimal
    end
end

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

    has_many :cookies
    has_many :users, :through => :cookies
end

I need to convert a User object to json, but I want it to include all cookies with are new (via new_cookies method), and embed in those cookies a) shortened_id and b) the id of it’s fortune.

Is this possible with to_json?

I have the follow so far which gives me the new cookies:

user.to_json :methods => :new_cookies

But I am stuck at trying to figure out how to include in the cookie object the value returned by method shortened_id and the cookies’s fortune’s id.

  • 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-15T02:19:25+00:00Added an answer on June 15, 2026 at 2:19 am

    You may override as_json in your user model.

    class User < ActiveRecord::Base
    
      def as_json(options={})
        json_res = super
        json_res['cookies'] = ...
      end
    
    end
    

    If these types of JSON customizations are needed in several places in your application, you should have a look at the jbuilder gem for rendering your JSON.

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

Sidebar

Related Questions

I have a two models set up like this: class User < ActiveRecord::Base #
I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class
I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I have my models & associations currently setup like so: class User < ActiveRecord::Base
I have two models, User and TrainingSession: class User < ActiveRecord::Base has_many :training_sessions end
So I have the following models in my program: class User < ActiveRecord::Base has_many

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.