0

I have an object (Institution) and I want to get the 2 arrays (marks and attachments) that are relationed with this object using JSON.

To be clear: For 1 institution, I have 3 marks and for every mark I have an attachment.

Here's my code of JSON file:

if @data['admin_institution']
    json.extract! @data['admin_institution'], :id, :name, :phone, :address, :site, :created_at, :updated_at
    if @data['admin_institution'].marks
        json.marks @data['admin_institution'].marks
        json.array!(@data['admin_institution'].marks) do | admin_mark|
            json.attachment admin_mark.attachment
        end
    end

else
    json.set! :response do
        json.set! :error, 'Not Found!'
    end
end

I want to reproduce something like this:

{
   id: 14,
   name: "Ins3",
   phone: "793215-2555",
   address: "lreewrwklkr",
   site: "lkerlke.com",
   created_at: "2016-03-01T14:00:37.000-03:00",
   updated_at: "2016-03-01T14:00:37.000-03:00",
   - marks: [
      - {
           id: 17,
           admin_attachment_id: 927,
           admin_bookmark_id: 3,
           admin_institution_id: 14,
           created_at: "2016-03-01T14:00:37.000-03:00",
           updated_at: "2016-03-01T14:00:37.000-03:00"
        },
        {
           id: 18,
           admin_attachment_id: 945,
           admin_bookmark_id: 1,
           admin_institution_id: 14,
           created_at: "2016-03-01T14:00:37.000-03:00",
           updated_at: "2016-03-01T14:00:37.000-03:00"
        }
      ],
   - attachment: {
         id: 927,
         name: "nature-16",
         title: "Nature-16",
         description: null,
         mime_type: "image/jpeg",
         url: "/uploads/nature-16.jpg",
         created_at: "2016-02-29T09:21:09.000-03:00",
         updated_at: "2016-02-29T09:21:09.000-03:00"
     }   
}

Instead, I'm getting only the values of the last array (attachment). Thanks in advance.

UPDATE:

I used through-association on my institution model then I could get the attachments "directly" through the marks, without do a loop of marks. The following code is giving me almost all that I want.

if @data['admin_institution']
    json.extract! @data['admin_institution'], :id, :name, :phone, :address, :site, :created_at, :updated_at, :marks, :attachments

else
    json.set! :response do
        json.set! :error, 'Not Found!'
    end
end

It's returning the Institution, the marks and the attachments, BUT not nested. I want marks inside the institution and attachments inside the marks. How can I make it work?

developer033
  • 24,267
  • 8
  • 82
  • 108

1 Answers1

0

Try this:

if @data['admin_institution']
    ...
    if @data['admin_institution'].marks
        json.marks @data['admin_institution'].marks do | admin_mark|
            json.attachment admin_mark.attachment
        end
    end

else
    ...
end
Ali Sepehri.Kh
  • 2,468
  • 2
  • 18
  • 27
  • Thanks for the response, but the marks didn't came. ` { id: 15, name: "kjerkerkjer", phone: "793215-2555", address: "kjasdkjadkjaskjd", site: "oedkf.com", created_at: "2016-03-02T11:15:15.000-03:00", updated_at: "2016-03-02T11:15:15.000-03:00", marks: [ { attachment: { id: 1101, name: "nature-24", title: "Nature-24", description: null, mime_type: "image/jpeg", url: "/uploads/nature-24.jpg", admin_attachment_id: null, admin_image_size_id: null, created_at: "2016-03-02T11:06:10.000-03:00", updated_at: "2016-03-02T11:06:10.000-03:00" } } ] } ` – developer033 Mar 02 '16 at 14:33
  • @developer033 Check your if clause. – Ali Sepehri.Kh Mar 02 '16 at 15:47
  • There's no error in my `if`clause, I already remove it to see what happens, but nothing changes. `JSON` is just returning the institution and the marks. – developer033 Mar 02 '16 at 15:51