简体   繁体   English

在嵌套模型上休息Api Ruby on Rails

[英]Rest Api Ruby On Rails Nested Models

I'm kinda new to the whole rails/ruby thing. 我对整个轨道/红宝石有点陌生。 I've built a restful API for an invoicing app. 我为开发票应用程序构建了一个宁静的API。 Summary of models is below. 模型摘要如下。

class Invoice < ActiveRecord::Base
    has_many :pages
end

class Page < ActiveRecord::Base
    belogs_to :invoice
    has_many :rows
end

class Row < ActiveRecord::Base
    belogs_to :page
end

I would like to be able to include related models in one rest call. 我希望能够在一个电话会议中包含相关模型。 I can currently do one level of nesting. 我目前可以进行一级嵌套。 For example i can get an Invoice with all its pages /invoices?with=pages is the call i would make. 例如,我可以获取包含其所有页面的/invoices?with=pages是我要拨打的电话。 In controller i would create a hash array from this as per below(probably not the best code you've seen): 在控制器中,我将根据以下内容从中创建一个哈希数组(可能不是您看到的最佳代码):

def with_params_hash
  if(params[:with])
    withs = params[:with].split(',')
        withs.map! do |with|
        with.parameterize.underscore.to_sym
    end
    withs
  else
    nil
  end
end

This will return a hash as array eg [:pages] 这将以数组形式返回哈希,例如[:pages]

In the controller i use it as 在控制器中,我将其用作

@response = @invoice.to_json(:include => with_params_hash)

This works fine. 这很好。 I would like to be able to include nested models of say page. 我希望能够包含说页面的嵌套模型。

As you know this can be done this way: 如您所知,可以通过以下方式完成此操作:

@invoice.to_json(:include => [:page => {:rows}])

The first question i guess is how do i represent this in the URL? 我猜的第一个问题是如何在URL中表示这个? I was thinking: /invoices?with=pages>rows . 我在想: /invoices?with=pages>rows Assuming thats how I decide to do it. 假设多数民众赞成在我决定这样做。 How do i then convert with=pages>rows into [:pages => {:rows}] 然后如何将with=pages>rows转换为[:pages => {:rows}]

Why don't you use jbuilder? 为什么不使用jbuilder? Will be easiest and you will can nest all models you want. 这将是最简单的,您可以嵌套所需的所有模型。

https://github.com/rails/jbuilder https://github.com/rails/jbuilder

So i ended up going with the format below for url: 所以我最终选择了以下网址格式:

/invoices?with=pages>rows

The function below will generate the function required: 下面的函数将生成所需的函数:

def with_params_hash
    final_arr = []
    with_array = params[:with].split(',')
    with_array.each do |withstring|
        if withstring.include? ">"
            parent = withstring[0..(withstring.index('>')-1)].parameterize.underscore.to_sym
            sub = withstring[(withstring.index('>')+1)..withstring.length].parameterize.underscore.to_sym
            final_arr << {parent => {:include => sub}}
        else
            final_arr << withstring.parameterize.underscore.to_sym
        end
    end
    final_arr
end

Usage in the controller looks like: 控制器中的用法如下所示:

@invoice.all.to_json(:include => with_params)

Alternatively as per @DavidGuerra's idea https://github.com/rails/jbuilder is not a bad option. 另外,按照@DavidGuerra的想法, https://github.com/rails/jbuilder也不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM