Wednesday 7 August 2013

Accessing attributes from other model

Accessing attributes from other model

I have 3 models : User, Location, Picture. A Location has several
pictures, and a User can provide several pictures. I use rabl to render
the result of my query in json. My problem is that I don't know how to
display the user name from the user id in this relation ?
I designed my models like that :
Picture :
class Picture < ActiveRecord::Base
...
belongs_to :location
belongs_to :user
...
end
User :
class User < ActiveRecord::Base
...
has_many :pictures
accepts_nested_attributes_for :pictures
attr_accessible :name, :email
...
end
Location :
class Location < ActiveRecord::Base
...
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures
attr_accessible :name, :address
...
end
My rabl file :
object false
collection @locations
attributes :id, :name, :address
child :pictures do
attributes :id, :location_id, :url, :user_id, :created_at
end
I tried to add :user_name in the child :pictures block, but obviously it
fails... How can I access it ?
Thanks !

No comments:

Post a Comment