ruby - Rails ajax return model no-db field -


my app built on rails 4. have model mem contains custom field realname:

class mem < activerecord::base     def realname        'hello'      end end 

now,i post ajax request form client mems list,include field realname:

def    render json: {mem.all} end 

but returned data didn't include realname,why? , how can realize this? thanks!

when pass single model or collection through, uses to_json format it. default, grab database attributes only. can tell grab other data doing this:

render json: mem.all.to_json(methods: :realname) 

can read more on how use method here: http://apidock.com/rails/activerecord/serialization/to_json. linked method deprecated, options same.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -