views:

97

answers:

2

Hi folks,

I have three models (simplified here):

class Child < ActiveRecord::Base
  has_many    :childviews, :dependent => :nullify
  has_many    :observations, :through => :childviews  
end
class Childview < ActiveRecord::Base
  belongs_to  :observation
  belongs_to  :child
end
class Observation < ActiveRecord::Base
  has_many    :childviews, :dependent => :nullify
  has_many    :children, :through => :childviews
end

I'm sending this to some JavaScript using Rails' to_json method like this:

render :layout => false , :json => @child.to_json(
  :include => {
    :observations => {
      :include => :photos, 
      :methods => [:key, :title, :subtitle]
    }
  },
  :except => [:password]
)

This works perfectly. Observations are retrieved fine 'through' the join table (childviews).

However, I also want to get at data that sits in the childviews join table; specifically the value for 'needs_edit'.

I can't figure out how to get at this data in a to_json call.

Can anyone help me? Many thanks in advance.

qryss

+1  A: 

Not sure, but shouldn't this work?

@child.to_json(
  :include => {
    :observations => {
      :include => :photos, 
      :methods => [:key, :title, :subtitle]
    },
    :childviews => { :only => :needs_edit }
  }, 
  :except => [:password]
)

EDIT: This might work too, since childviews belongs_to the overvation:

@child.to_json(
  :include => {
    :observations => {
      :include => { :photos, :childviews => { :only => :needs_edit } } 
      :methods => [:key, :title, :subtitle]
    }
  }, 
  :except => [:password]
)
Rock
Hey Rock,Thanks for the help.That's nearly right (and good enough for me to get going). What I get from that is an array of 'needs_edit' flags that I can map onto the array of observations.What I was hoping to get is the 'needs_edit' flag actually in the JSON hash for the relevant observation. Not crucial; I can live with what you've given me so thank you for getting me to this point!qryss
qryss
Oh, and you have a slight typo: '=>', not '='.
qryss
Maybe you can put the :childviews => {:only=>:needs_edit} inside the :obervations hash? (see edit) That should include the needs_edit value, for the given observation, but I'm not sure.
Rock
A: 

Hey there,

Thanks to Rock for the pointers - I now have it working!

This code:

@child.to_json(:include => 
  {
    :observations => {
      :include => {
        :photos => {},
        :childviews => {:only => :needs_edit}
      }, 
      :methods => [:S3_key, :title, :subtitle]
    }     
  },
  :except => [:password]
)

gives me this output (abbreviated for clarity):

{
    "child":
    {
        "foo":"bar",
        "observations":
        [
            {
                "foo2":"bar2",
                "photos":
                [
                    {
                        "foo3":"bar3",
                    }
                ],
                "childviews":
                [
                    {
                        "needs_edit":true
                    }
                ]
            }
        ]
    }
}

Thank you, Rock! That was doing my head in.

:)

qryss

qryss