How to set a child association template using acts_as_api gem in Rails
I'm working on a project that deals with Users and Events. An user can
create one or more events. Besides, several users can attend to one or
more events. My problem comes with this many-to-many relationship. I'm
using a join table to keep this. The entity is called Attenders and keeps
the id of a User and the corresponding Event which he/she is attending. Mi
problem comes when I try to send a JSON response through the index or show
controller in Rails. My (simplified) models are like this:
Event
class Event include Mongoid::Document
field :title, type: String
field :date, type: String
field :place, type: String
field :description, type: String
acts_as_api
include Templates::Event
include Templates::Attender
# ...
# VALIDATIONS
# ...
belongs_to :user
has_many :attendings, class_name: "Attender", inverse_of: :attendingTo
end
User
class User
include Mongoid::Document
authenticates_with_sorcery!
field :name, type: String
field :surname, type: String
field :email, type: String
field :gender, type: String
field :age, type: Integer
field :crypted_password, type: String
field :salt, type: String
field :password, type: String
field :password_confirmation, type: String
acts_as_api
include Templates::User
include Templates::Attender
# ...
# VALIDATIONS
# ...
has_many :events
has_many :attendances, class_name: "Attender", inverse_of: :attendanceOf
end
Attender
class Attender
include Mongoid::Document
acts_as_api
include Templates::User
include Templates::Event
include Templates::Attender
belongs_to :attendanceOf, class_name: "User", inverse_of: :attendances
belongs_to :attendingTo, class_name: "Event", inverse_of: :attendings
end
And now, my acts_as_api templates, separated in different files, are like
these:
Event
module Templates::Event
extend ActiveSupport::Concern
included do
api_accessible :embedded_event do |response|
response.add :id
response.add :title
response.add :date
response.add :place
response.add :description
end
api_accessible :general_event, :extend => :embedded_event do |response|
response.add :user_id
response.add :attendings, :template => :attender_event
end
end
end
User (counterpart)
Attender
module Templates::Attender
extend ActiveSupport::Concern
included do
api_accessible :attender_user do |response|
response.add :attendanceOf
end
api_accessible :attender_event do |response|
response.add :attendingTo
end
end
end
My index action of Rails controller is like this:
Events_controller
class EventsController < ApplicationController
self.responder = ActsAsApi::Responder
respond_to :json
def index
e = Event.all
respond_with e, :api_template => :general_event, status: :ok
end
# ...
# OTHER ACTIONS
# ...
end
Well, with all these, when I call my index action from my Ember
application (which except for this is working fine), the response that
Firefox console is returning me says:
NoMethodError in EventsController#index
undefined method `attendingTo' for #<Event:0xb61ac924>
I have tried several different combinations of my templates but I'm really
stuck on here. Any hand would be really useful on this. Thank you guys a
lot in advance.
No comments:
Post a Comment