Rails how to change attribute name when rendering json?
You can do this with a one-line method in Pakke
:
def as_json(*args)
super.tap { |hash| hash["name"] = hash.delete "navn" }
end
Calling super
will generate json hash as usual, then before it's returned you'll swoop in and change the key of the "navn" entry.
Change JSON attribute name on POST in Rails?
You can try this
Suppose your params are
params = { post: {
name: 'war', users_attributes: [
{ title: 'Ruby documentation browser!' },
{ title: 'the modern citizen' },
{ title: '', destroy: '1' }]}}
users = params.values[0].delete(:users_attributes)
params.values[0][:users] = users
in rails json rendering, how to show a different key name for a particular attribute
If you're able to add an attribute accessor for _id
called just id
, then this should be easily solved by overriding as_json
in your model.
def id
self._id
end
def as_json(options={})
options.merge!(:except => :_id, :methods => :id)
super(options)
end
Update: Made the override a bit more friendly to the parent method.
Rails Adding Attributes to JSON Serializer
class UserSerializer
def initialize(user)
@user=user
end
def to_serialized_json(*additional_fields)
options ={
only: [:username, :id, *additional_fields]
}
@user.to_json(options)
end
end
each time you want to add new more fields to be serialized, you can do something like UserSerializer.new(@user).to_serialized_json(:token, :errors)
if left empty, it will use the default field :id, :username
if you want the json added to be customizable
class UserSerializer
def initialize(user)
@user=user
end
def to_serialized_json(**additional_hash)
options ={
only: [:username, :id]
}
@user.as_json(options).merge(additional_hash)
end
end
UserSerializer.new(@user).to_serialized_json(token: @token, errors: @user.error.messages)
if left empty, it will still behaves like the original class you posted
Changing attribute name in record objects
You can override the name in the as_json
method of the Group
class:
def as_json(*args)
super.tap { |hash| hash["group_id"] = hash.delete "id" }
end
See:
Rails how to change attribute name when rendering json?
rails render json add custom attribute
Adding a method to my model called message
that returns a string and then calling,
render :json => Group.find(params[:id]), :methods => :message
did the trick.
Ruby on Rails render json: ignoring alias_attribute
I wouldn't expect render json: @accounts
to include the aliased attributes at all. The alias_attribute
just gives you the luxury of referring to the attribute with another name - it doesn't replace the original name at all.
If you do want to include the aliases in your json output for a model you can override as_json
and add those methods explicitly:
def as_json(options = {})
options[:methods] ||= []
options[:methods] += [:name, :description]
super(options)
end
(I've deliberately omitted :id
as that may be a special case - not entirely sure and can't test locally at the moment)
Related Topics
You Don't Have Write Permissions into the /Var/Lib/Gems/1.9.1 Directory
Initialize a Ruby Class from an Arbitrary Hash, But Only Keys with Matching Accessors
Documentation for Psych To_Yaml Options
Remove Double Quotes from String
Sort Values Using a Specific Collation in Ruby/Rails
Ruby Errors with Os X Yosemite
Zentest Autotest Not Running Tests
Another Way Instead of Escaping Regex Patterns
How to Make Ruby's Restclient Gem Respect Content_Type on Post
Ruby Incorrectly Parses 2 Digit Year
How to Write Bom Marker to a File in Ruby
How to Read a Gzip File Line by Line
Rails: Detecting User Agent Works in Development But Not Production