ruby - How to return an object from two tables form a rails web serivce? -
i have 2 activerecord models: event , place. event has property name place_id points place. place has column named place_name.
how write web service returns json of events each event holds associated place_name value.
i tried adding attr_accessor field name place_name event model , populate manually after selecting events. when do: render :json => events_collection_result
the place_name doesn't appear in json.
all of logic falls under activerecord associations. it's fundamental piece of rails functionality, it's documented. guide should more suffice.
you'll want invoke belongs_to :place
if event
class definition. there, can access event's place name via @event.place.place_name
, although i'd recommend rename place_name
name
.
json won't display association default. there plenty of ways remedy this, basic of overwrite event#to_json
, method called default when rails attempts produce json representation of activerecord instance.
Comments
Post a Comment