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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:13:14+00:00 2026-06-12T10:13:14+00:00

How to override default deserialization of params to model object? In other words, how

  • 0

How to override default deserialization of params to model object?
In other words, how to make Rails understand camel case JSON with a snake case database?

Example: I receive params Foo object with a field fooBar and I want my Foo model to understand fooBar is in fact database field foo_bar.

"Foo": {
  "fooBar": "hello" /* fooBar is database field foo_bar */
}
class Foo < ActiveRecord::Base
  attr_accessible :foo_bar
end

class FoosController < ApplicationController
  def new
    @foo = Foo.new(params[:foo])
  end

Foo.new(params[:foo]) assumes params[:foo] contains foo_bar. Instead params[:foo] contains fooBar (in my case params contains JSON data).

I would like a clean way to handle this case, the same way a model can override as_json:

class Foo < ActiveRecord::Base
  attr_accessible :foo_bar, :another_field

  def as_json(options = nil)
    {
      fooBar: foo_bar,
      anotherField: another_field
    }
  end
end

There is a from_json method inside ActiveModel but it is not called when Foo.new(params[:foo]) is run.

I’ve read several times that overriding initialize from a model object is a terrible idea.

  • 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-12T10:13:15+00:00Added an answer on June 12, 2026 at 10:13 am

    I’ve checked active_model_serializers, RABL and JBuilder. None of them allow to customize the JSON format that is received.

    For that one must deal with wrap_parameters, see http://edgeapi.rubyonrails.org/classes/ActionController/ParamsWrapper.html
    It works, still the code is ugly: I get JSON stuff inside my controller + the serializer/model instead of one place.

    Example of use of wrap_parameters:

    class EventsController < ApplicationController
      wrap_parameters :event, include: [:title, :start, :end, :allDay, :description, :location, :color]
    
      def create
        respond_with Event.create(params[:event])
      end
    end
    

    and then inside my model (Frederick Cheung is right on this part):

    class Event < ActiveRecord::Base
      attr_accessible :title, :start, :end, :allDay, :description, :location, :color
    
      # JSON input allDay is all_day
      alias_attribute :allDay, :all_day
    
      # JSON input start is starts_at
      # +datetime+:: UNIX time
      def start=(datetime)
        self.starts_at = Time.at(datetime)
      end
    
      # JSON input end is starts_at
      # +datetime+:: UNIX time
      def end=(datetime)
        self.ends_at = Time.at(datetime)
      end
    
      # Override the JSON that is returned
      def as_json(options = nil)
        {
          id: id,
          title: title,
          start: starts_at, # ISO 8601, ex: "2011-10-28T01:22:00Z"
          end: ends_at,
          allDay: all_day,
          description: description, # Not rendered by FullCalendar
          location: location,
          color: color
        }
      end
    end
    

    For info ASP.NET MVC (with Json.NET) does it using C# decorator attributes which is pretty elegant:

    class Post
    {
        [JsonPropertyAttribute("title")]
        public string Title;
    }
    

    I have created a gist that shows how to implement serialization/deserialization: https://gist.github.com/3858908

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

Sidebar

Related Questions

i have the following function to override the default save function in a model
How can i override default make parameters , which is: make -w Each time
Is there any XForms object which I could use to override default evaluation context
I'm trying to override the default category view in Satchmo. I'm successfully overriding other
how to set custom or override default model relations configurations such as foreign key,
I have some namespaced models which I'm trying to override the default rails table
I'm trying to override the default User model in Django to add some logic
How can I override default behavior of displaying item label on the input when
I want to override the default integer constructors in Haskell so they produce strings
I am trying to override the default img tag style when a category is

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.