Hi,
i have a "place" model with 2 columns (id, name).
I'm calling an external api which gives me data to create a new place
object.

I would like to add the address from the external place to the place
object and render it as a json result, without storing it in the
database.

i don't get any errors, but the address never shows up within the json
output. i've tried with attr_accessor but that didn't work either.

any hints?

thanks


==========================================
place.rb

class Place < ActiveRecord::Base
attr_accessor: address
end

==========================================
places_controller.rb

external_api_place_data = @client.find(place_id)
place = Place.new
place.name = place.name
place.id = place.id

//not showing up in the json result
place.address = external_api_place_data.address

places = []
places << place

respond_to do |format|
format.html # index.html.erb
format.json { render json: places }
end



==========================================

--
Posted via http://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 at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Frederick Cheung at Oct 25, 2011 at 1:13 pm

    On 25 Oct 2011, at 14:03, "Lukas M." wrote:

    Hi,
    i have a "place" model with 2 columns (id, name).
    I'm calling an external api which gives me data to create a new place
    object.

    I would like to add the address from the external place to the place
    object and render it as a json result, without storing it in the
    database.

    i don't get any errors, but the address never shows up within the json
    output. i've tried with attr_accessor but that didn't work either.
    foo.to_json(:methods => [:attr1, :attr2])
    Maybe? (assuming that foo.attr1 returns one of your extra attributes)

    Fred.


    any hints?

    thanks


    ==========================================
    place.rb

    class Place < ActiveRecord::Base
    attr_accessor: address
    end

    ==========================================
    places_controller.rb

    external_api_place_data = @client.find(place_id)
    place = Place.new
    place.name = place.name
    place.id = place.id

    //not showing up in the json result
    place.address = external_api_place_data.address

    places = []
    places << place

    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: places }
    end



    ==========================================

    --
    Posted via http://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 at http://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.
  • Lukas M. at Oct 25, 2011 at 1:34 pm
    hi,
    that works but not in the case when i would like to do something like
    this:


    user_place = Location.new(:place => place, :user => user)

    user_place.to_json //address is missing

    --
    Posted via http://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 at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Tim Shaffer at Oct 25, 2011 at 1:52 pm
    I think for Rails to recognize that the attribute should be included, you
    need to use attr_accessible rather than the Ruby method attr_accessor.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/K5aXBvoKXwcJ.
    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.
  • Lukas M. at Oct 25, 2011 at 3:01 pm
    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.

    --
    Posted via http://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 at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Frederick Cheung at Oct 25, 2011 at 3:17 pm

    On 25 Oct 2011, at 16:00, "Lukas M." 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 via http://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 at http://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.
  • Peter Vandenabeele at Oct 25, 2011 at 3:17 pm

    On Tue, Oct 25, 2011 at 5:00 PM, Lukas M. 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.
    From the top of my head (not hard tested now).
    Now you refer to to_yaml, in previous mails, you refered to to_json.

    Be careful, there is a fundamental difference between to_xml, to_json,
    to_yaml on ActiveRecord ...

    One of them (I believe to_yaml) prints the "raw" database column values,
    even if you overwrite that accessor function with a name. The other two
    (to_json, to_xml IIRC) use the attribute accessors (from ActiveRecord I
    presume), that can be overwritten by your own code.

    So, I would expect to_yaml to never work in this scenario (where there is
    no database column). But to_json should be possible in this scenario
    (I did some simple tests with ActiveModel and that seemed to work as
    expected).

    HTH,

    Peter

    --
    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.
  • Jeff Lewis at Oct 25, 2011 at 4:43 pm
    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 wrote:
    On 25 Oct 2011, at 16:00, "Lukas M." 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 25, '11 at 1:03p
activeOct 25, '11 at 4:43p
posts8
users5
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase