On Tuesday, January 14, 2014 12:07:57 PM UTC-8, Andrew Burleson wrote:
>
> For what it's worth I was about to post a question about the same issue. 
> In my case what I'm finding is basically this:
>
> If you set up two Sequel Models, each with the json serializer plugin, 
> then you do a join query, it will fail with no method error on the joined 
> model's methods.
>
> Here's my situation:
> I have two models, Edition and Access. Both classes call `plugin 
> :json_serializer` at the top.
>
> If do this this:
>
>     result = Access.join(:editions, :id => :context_id).where(:user_id => 
> 57)
>     result.to_json
>
> I always get NoMethodError on Edition attributes. For instance, 
> "NoMethodError: 
> undefined method `book_id' for #<Access:0x007f9c014af0c0>" (where book_id 
> is actually an Edition attribute).
>
> If I call it this way:
>
>     result = Edition.join(:accesses, :context_id => :id).where(:user_id => 
> 57)
>     result.to_json
>
> I get the same NoMethodError but on Access attributes. For instance, 
> "NoMethodError: 
> undefined method `resource_id' for #<Edition:0x007f9c01d2f0d8>" (where 
> resource_id is actually an Access attribute).
>
> I tried including the association as well:
>
>     result.to_json(:include => :access)
>
> but that didn't work either.
>
> Now, as I'm typing this up I'm thinking about something, the associations 
> not declared in these classes as we're using Sequel only to read an old 
> database, so not every table has a Sequel Model and not all the plumbing is 
> necessary. Is that likely the issue, or is something else going on here?
>

Well, if you want the json_serializer to work, you need to have 
associations defined, and then do something like:

 Edition.where(:user_id => 57).to_json(:include=>:accesses)

Note the lack of joining.

The NoMethodErrors you are running into are due to the fact that you have 
entries in the values for columns that you don't have Edition instance 
methods for (because the columns are from the accesses table).

If you did really want to mix the access and edition columns in the same 
hash, you could do:

Edition.join(:accesses, :context_id => :id).where(:user_id => 
57).all.to_json

But I doubt that is what you want.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to