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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:24:20+00:00 2026-06-07T12:24:20+00:00

Given two models and a controller: Apples class Apples < ActiveRecord::Base belongs_to :not_oranges …

  • 0

Given two models and a controller:

Apples

class Apples < ActiveRecord::Base
  belongs_to :not_oranges
  ...
  def as_json(options={})
    opts = {:include => [:not_oranges]}
    super(options.reverse_merge! opts)
  end
end

Oranges

class Oranges < ActiveRecord::Base
  belongs_to :not_apples
  ...
  def as_json(options={})
    opts = {:include => [:not_apples]}
    super(options.reverse_merge! opts)
  end
end

Search Controller

class SearchController < ApplicationController
  a = Apples.search params[:q]
  o - Oranges.search params[:q]

  @results = {
    :apples => a,
    :oranges => o
  }

  respond_to do |format|
    format.json { render :json => @results }
end

As you can see, the two models are completely unrelated and both have different :include options in their as_json definitions.

All works as expected if the search query only hits apples or only hits oranges, but once both objects aren’t empty I get:

undefined method `not_apples' for #<Oranges:0x00000004af8cd8>

Seems either the two as_json definitions are being merged, or Oranges.as_json is being overriden by Apples.as_json.

Is this expected behaviour? Is there any clean way around it without using something like RABL? I feel it would be overkill for my needs.

  • 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-07T12:24:22+00:00Added an answer on June 7, 2026 at 12:24 pm

    In pseudo code the code for hash as_json method looks like

    def as_json(options={})
      Hash[collect {|key,element| [key.to_s,element.as_json(options)]}]
    end
    

    But your element is modifying the options argument you pass to it. Hash is unaware of this and so passes the modified options hash to as json.

    It’s usually a good idea not to modify in place the arguments passed to you, except when it is very clear this is ok. I’d rewrite your method as

    def as_json(options={})
      defaults = {:include => :not_apples}
      super(defaults.merge(options))
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

models: #StatusMessage model class StatusMessage < ActiveRecord::Base belongs_to :users default_scope :order => created_at DESC
Given two methods on the same class in Java : public void doSomething( Person
Given I have two models: company and location . A company has many locations
I have two models, referral and user model. Referral belongs_to User AND user has_one
I have the model Post: class Post < ActiveRecord::Base has_one :location, :dependent => :destroy
Given two model classes, Foo and Bar , I want Foo to have 3
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
Given two identical boost::variant instances a and b , the expression ( a ==
Given: two images of the same subject matter; the images have the same resolution,
Given two dates, what is the best method to calculate the number of days

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.