gpt4 book ai didi

ruby-on-rails - ActiveRecord::Serializer 中的条件侧载数据(基于 Controller 操作)

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:15 24 4
gpt4 key购买 nike

我在 Rails 和构建应用程序方面没有太多经验。我尝试使用 ActiveModel::Serializers 构建 API。

为特定事件操作有条件地加载数据的最佳方法是什么?

我是否必须通过在每次调用时发送查询参数来做到这一点,或者我可以设置 includes:true only for specific action or any other suggestion?

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]

# GET /events
def index
if params.has_key?("mode") and params[:mode] == "owned"
own_event
else
# @events = Event.all
render json: @events
end
end

# GET /events/1
def show
render json: @event
end

# GET /events/new
def new
@event = Event.new
end

# GET /events/1/edit
def edit
end

# POST /events
# POST /events.json
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

# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
if @event.update(event_params)
render json: @event
else
render json: @event.errors, status: :unprocessable_entity
end
end

# DELETE /events/1
# DELETE /events/1.json
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
# else
# render json: {error: 1, message: "Events not found."}, status: 404
# end
end

def locate_collection
if (params.has_key?("filter"))
@events = EventPolicy::Scope.new(current_user, Event).resolve(filtering_params)
# @event_orders = EventOrder.filter(filtering_params)
else
@events = policy_scope(Event)
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.


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.

我能为此做什么?

最佳答案

考虑到您使用的是 AMS 版本 0.8.x,

您可以像这样创建一个 base_serializer.rb 并扩展它。

class BaseSerializer < ActiveModel::Serializer
def include_associations!
if @options[:embed]
embed = @options[:embed].split(',').map!(&:to_sym)
embed.each do |assoc|
include! assoc if _associations.keys.include?(assoc)
end
end
end
end

class EventSerializer < BaseSerializer

而EventsController中的index方法可以写成

# GET /events?embed=registration_centers,tickets
def index
if params.has_key?("mode") and params[:mode] == "owned"
own_event
else
# @events = Event.all
render json: @events, embed: params[:embed]
end
end

我刚刚向您展示了可以在 url 参数中添加所需的关联。您可以想出一些聪明的方法来发送参数,这样您就不需要将它们添加到 url 请求中。

关于ruby-on-rails - ActiveRecord::Serializer 中的条件侧载数据(基于 Controller 操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28364414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com