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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:08:56+00:00 2026-06-16T12:08:56+00:00

I have a post model that has a virtual attribute that I would like

  • 0

I have a post model that has a virtual attribute that I would like to set and then include in a response to a JSON call to my post#index action. I can’t seem to get the virtual attribute to be included in the response.

class Post < ActiveRecord::Base
  attr_accessible :height
  attr_accessor :m_height
end

class PostsController < ApplicationController
  respond_to :html, :json, :js

  def index
    story = Story.find(params[:story_id])
    @posts = story.posts.where("posts.id >= ?", 100)
    @posts.each do |post|
      post.m_width = post.height * 200
    end
    results = { :total_views => story.total_views,  
                :new_posts => @posts }
    respond_with(results)
  end
end

I think that I must need something similar to @post.to_json(:methods => %w(m_width)), but I don’t see how to use :methods in a respond_with

  • 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-16T12:08:57+00:00Added an answer on June 16, 2026 at 12:08 pm

    This seems to provide the answer. Implement a to_json and to_xml in your models, as appropriate, with definitions like:

    There’s a better answer implied here.

    Following code stolen from the post:

      def as_json(options={})
        super(options.merge(:methods => [...], :only => [...], :include => [...])
      end
    

    to_json won’t be called on your model in this case, from what I can tell in the source, but as_json will be, in the process of serialization.

    So, here’s what happens, in overview form:

    1. You call respond_with with the results hash you’ve constructed.
    2. Rails (ActionController) calls to_json on that.
    3. to_json sends you over to JSON::Encoding which keeps calling as_json all the way down until everything is JSONified.

    That’s why there was the confusion about to_json and as_json in an earlier version of this answer.

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

Sidebar

Related Questions

Say you have a Post model that has a :title , :author , :content
I have a post controller that has many comments. The post model has a
I have a Post model object that has reference to a parent object. The
I have a model that looks something like this class Post(models.Model): id = models.IntegerField(unique=True,
I have a model that has counter_cache enabled for an association: class Post belongs_to
I have a Tag model that has validates_uniqueness_of :name, :case_sensitive => false and then
I have a Post model that has an associated Comment model. Each comment has
I have a model that has ratings in it for an post. I want
I have a post model that has many tags through taggings. let's say: @posts
I have a Post model that has a datetime.date field for posted_date . I

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.