this error usually occurs when trying to mix 1st and 2nd order
relationships in a single to_json call:
undefined method `macro'' for nil:NilClass
Rails 3 has this way to support 2nd order relationships:
http://apidock.com/rails/ActiveRecord/Serialization/to_json
So I try to use it:
@units = User.where(:id =>
params[:user_id]).first.units.to_json(:include => {
:only => :reports,
:notifications => {:include => {:only
=> :notification_codes} }
})
A unit has many reports and notifications. And a notification belongs
to notification code.
This is the relationship between notifications and notification codes:
//notifications
belongs_to :notification_code, :foreign_key => :code
//notification codes
has_many :notifications
So I am not sure why error occurs.
Thanks for response
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi! The call to to_json shouldn''t be the last call in the statement? Regards, Everaldo On Tue, Nov 15, 2011 at 9:06 PM, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> this error usually occurs when trying to mix 1st and 2nd order > relationships in a single to_json call: > > undefined method `macro'' for nil:NilClass > > Rails 3 has this way to support 2nd order relationships: > > http://apidock.com/rails/ActiveRecord/Serialization/to_json > > So I try to use it: > > @units = User.where(:id => > params[:user_id]).first.units.to_json(:include => { > :only => :reports, > :notifications => {:include => {:only > => :notification_codes} } > }) > > A unit has many reports and notifications. And a notification belongs > to notification code. > > This is the relationship between notifications and notification codes: > > //notifications > belongs_to :notification_code, :foreign_key => :code > > //notification codes > has_many :notifications > > So I am not sure why error occurs. > > Thanks for response > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It was working fine until I added the 2nd order relationship. The documentation isnt clear about how to add the 2nd order relationship On Nov 15, 6:20 pm, Everaldo Gomes <everaldo.go...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > The call to to_json shouldn''t be the last call in the statement? > > Regards, > Everaldo > > > > > > > > On Tue, Nov 15, 2011 at 9:06 PM, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote: > > this error usually occurs when trying to mix 1st and 2nd order > > relationships in a single to_json call: > > > undefined method `macro'' for nil:NilClass > > > Rails 3 has this way to support 2nd order relationships: > > >http://apidock.com/rails/ActiveRecord/Serialization/to_json > > > So I try to use it: > > > @units = User.where(:id => > > params[:user_id]).first.units.to_json(:include => { > > :only => :reports, > > :notifications => {:include => {:only > > => :notification_codes} } > > }) > > > A unit has many reports and notifications. And a notification belongs > > to notification code. > > > This is the relationship between notifications and notification codes: > > > //notifications > > belongs_to :notification_code, :foreign_key => :code > > > //notification codes > > has_many :notifications > > > So I am not sure why error occurs. > > > Thanks for response > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 15, 11:06 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> this error usually occurs when trying to mix 1st and 2nd order > relationships in a single to_json call: > > undefined method `macro'' for nil:NilClass > > Rails 3 has this way to support 2nd order relationships: > > http://apidock.com/rails/ActiveRecord/Serialization/to_json > > So I try to use it: > > @units = User.where(:id => > params[:user_id]).first.units.to_json(:include => { > :only => :reports, > :notifications => {:include => {:only > => :notification_codes} } > }) > > A unit has many reports and notifications. And a notification belongs > to notification code. >There is an example of second order include & only on the page you link to. You''re using :only to refer to association names whereas only is supposed to denote which attributes you want to show for a given object so i think you are confusing to_json. something like :include => {:reports, :notifications => :notification_codes} should do the trick. Fred :include => {:notifications, :only => :reports, :include => {:> This is the relationship between notifications and notification codes: > > //notifications > belongs_to :notification_code, :foreign_key => :code > > //notification codes > has_many :notifications > > So I am not sure why error occurs. > > Thanks for response-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thanks for response
THis here would produce syntax error:
:include => {:reports, :notifications => :notification_codes}
So I try this (to ensure :reports has a value):
{:include => {:reports => {}, :notifications => :notification_codes}}
And this is ultimately what it would look like:
User.where(:id => params[:user_id]).first.units.to_json({:include =>
{:reports => {}, :notifications => :notification_codes}})
But this will say:
can''t convert Symbol into Hash
On Nov 16, 3:14 am, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Nov 15, 11:06 pm, John Merlino
<stoici...-YDxpq3io04c@public.gmane.org> wrote:
>
>
>
>
>
>
>
>
>
> > this error usually occurs when trying to mix 1st and 2nd order
> > relationships in a single to_json call:
>
> > undefined method `macro'' for nil:NilClass
>
> > Rails 3 has this way to support 2nd order relationships:
>
> >http://apidock.com/rails/ActiveRecord/Serialization/to_json
>
> > So I try to use it:
>
> > @units = User.where(:id =>
> > params[:user_id]).first.units.to_json(:include => {
> > :only => :reports,
> > :notifications => {:include => {:only
> > => :notification_codes} }
> > })
>
> > A unit has many reports and notifications. And a notification belongs
> > to notification code.
>
> There is an example of second order include & only on the page you
> link to. You''re using :only to refer to association names whereas
only
> is supposed to denote which attributes you want to show for a given
> object so i think you are confusing to_json. something like :include
> => {:reports, :notifications => :notification_codes} should do the
> trick.
>
> Fred
>
> :include => {:notifications, :only => :reports, :include => {:
>
>
>
>
>
>
>
> > This is the relationship between notifications and notification codes:
>
> > //notifications
> > belongs_to :notification_code, :foreign_key => :code
>
> > //notification codes
> > has_many :notifications
>
> > So I am not sure why error occurs.
>
> > Thanks for response
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 16, 1:46 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> thanks for response > > THis here would produce syntax error: > > :include => {:reports, :notifications => :notification_codes} >oops, that should have been [:reports, {:notifications => :notification_codes}] also, if notification belongs_to :notification code, then it should be [:reports, {:notifications => :notification_code}] i.e. the symbols in that hash should exactly match the name given to the has_many, belongs_to etc. Fred> > > Thanks for response-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #1032161:> On Nov 16, 1:46pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote: >> thanks for response >> >> THis here would produce syntax error: >> >> :include => {:reports, :notifications => :notification_codes} >> > oops, that should have been [:reports, {:notifications > => :notification_codes}] > > also, if notification belongs_to :notification code, then it should be > > [:reports, {:notifications => :notification_code}] > > i.e. the symbols in that hash should exactly match the name given to > the has_many, belongs_to etc. > > Fredthanks for response, this is what ultimately worked for me: Unit.where(:id => params[:unit_id]).first.reports.to_json(:include => { :notifications => { :include => { :notification_code => { :only => :name } } } }) As you mentioned, this would only include the "name" attribute of notification code, but anything else I tried was still giving the "macro" error. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.