Tuesday, October 25, 2011

[Rails] Re: additional model attributes

One alternative in terms of dynamically adding an attribute to a model
instance (without having to have pre-defined any attr_... in the
class):

...
place = Place.new
place.name = place.name
place.id = place.id
place['address'] = external_api_place_data.address
...

From then on, for that instance, you can call place.address, and it
will show up in place.to_json (since to_json is generated from
place.attributes, which now has a k/v for address in attributes).

Jeff

On Oct 25, 8:16 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> On 25 Oct 2011, at 16:00, "Lukas M." <li...@ruby-forum.com> wrote:
>
> > hm.. no luck with that either. when i try to print out the places object
> > in the console (logger.debug @place.to_yaml),  "address" is missing.
>
> For json at least you could override the as_json method to add the attributes you want.
>
> Fred
>
>
>
>
>
>
>
>
>
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> > To post to this group, send email to rubyonrails-talk@googlegroups.com.
> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate