I do not have much experience in rails and building an application. I tried to create an API using ActiveModel :: Serializers.
What is the best way to conditionally load data laterally for a particular event?
Do I need to do this by sending request parameters with each call, or can I set: true only for a specific action or any other sentence?
class EventsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
before_filter :locate_collection, :only => :index
skip_before_filter :verify_authenticity_token, :only => [:create, :index, :show]
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
if params.has_key?("mode") and params[:mode] == "owned"
own_event
else
render json: @events
end
end
def show
render json: @event
end
def new
@event = Event.new
end
def edit
end
def create
@event = Event.new(event_params)
@event.creator_user_id = current_user.id
if @event.save
render json: @event
else
render json: @event.errors, status: :unprocessable_entity
end
end
def update
if @event.update(event_params)
render json: @event
else
render json: @event.errors, status: :unprocessable_entity
end
end
def destroy
aa = @event.destroy
render json: aa
end
def own_event
@events = Event.where(creator_user_id: current_user.id)
if @events.count > 0
render json: @events
else
render json: []
end
end
def locate_collection
if (params.has_key?("filter"))
@events = EventPolicy::Scope.new(current_user, Event).resolve(filtering_params)
else
@events = policy_scope(Event)
end
end
private
def set_event
@event = Event.find(params[:id])
end
def filtering_params
params.slice(:event_type_id)
end
end
**My Event serializer**
It includes data for multiple association listed below. i don't want to show all the data with event call.
class EventSerializer < ActiveModel::Serializer
attributes :id, :event_name, :event_start_date, :event_end_date, :creator_user_id, :event_proposal_id, :daily_start_time, :daily_start_time, :payment_category, :total_capacity, :contact_email, :description, :graced_by, :contact_details, :video_url, :demand_draft_instructions, :status, :cannonical_event_id, :website, :event_type_id, :additional_details, :event_start_time, :event_end_time, :committee_id
embed :ids
has_one :address, include: true
has_many :tickets, include: true
has_many :event_cost_estimations, include: true
has_many :event_seating_category_associations, include: true
has_many :event_awarenesses, include: true
has_one :pandal_detail, include: true
has_one :bhandara_detail, include: true
has_many :event_tax_type_associations, include: true
has_many :event_team_details, include: true
has_one :event_type, include: true
# has_many :event_registration_center_associations, include: true
has_many :registration_centers, include: true
# has_many :event_registrations, include: true
has_many :event_orders, include: true
has_one :venue_type
end
In event serializer i have includes :true for sideloaded data and I want to show sideloaded data(includes: true) of registration_centers and tickets only for index action.
What can I do for this?
Thanks in advance!