简体   繁体   中英

How to combine more than one models in JBuilder?

The structure I need is:

[{model}, {model}, {other model}, ...] # for jquery ui autocomplete

How can I build this structure with JBuilder? For render array they has array! method, that accepts one argument - collection. But I have more than one collection. Why it's so complicated?

I used the method child! to solve the problem.

  json.models do 
    @model1s.each do |m1|   
      json.child!{|json| json.partial! m1,as :Model1}        
    end

    @model2s.each do |m2|   
      json.child!{|json| json.partial! m2,as :Model2}   
    end
  end

Get more from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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