Note that there are some explanatory texts on larger screens.

plurals
  1. PORoR nested :include to include sub-resources in to_xml/to_json
    text
    copied!<p>I have a weird data model situation to start with, so maybe my whole approach is wrong. Here's what I'm doing:</p> <p>I have a class called Bird and a simple class called Color. Conceptually, each bird has two to_many associations to Color, one for male colors and one for female colors. The way I've handled this is to use a join model called BirdColoration that belongs to a bird and a color and has an additional boolean field to tell if the coloration is for male or female. So each bird actually has a to_many relationship to BirdColoration as well as a to_many to Color :through BirdColoration. If this sounds reasonable, then continue reading. Otherwise, stop and tell me why it's wrong!</p> <p>I need to be able to dump the birds table as json. Previously, when each bird only had one to_many association to colors, I could just use :include to include each bird's colors in the json dump. Now, I'm including the BirdColorations in the dump, but I still need to get at the color models themselves. I could separately include each bird's colors and colorations and then match them up while parsing, but I would much rather just include each coloration's color directly. Something like </p> <pre><code> format.json { render :json =&gt; @birds.to_json(:include =&gt; [{:bird_colorations =&gt; :color}, :seasons, :habitats, :image_holders]) } </code></pre> <p>The above doesn't work, however. I think that this should be possible. Can anyone point me in the right direction for how to handle this?</p> <p>For now, I'll just include each bird's color and colorations separately and match them up in parsing. At least I know that will work.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload