On Jan 5, 4:13 pm, cult hero <[email protected]> wrote:
> All righty... I have run into some kind of weird problem that I can't
> explain. It might be a bug, but I dunno.
>
> I have three models: Article, ArticleSource and ArticleContent. This
> correspond to following tables and primary keys: articles (id),
> article_sources (id), article_contents (language_id, article_id)
>
> I need bits and pieces of each model to create a list that's returned
> so people can browse articles by source or other means.
>
> Article has the following associations:
>
>   many_to_one :source,
>     :class => :ArticleSource,
>     :key => :source_id,
>     :graph_join_type => :inner
>
>   many_to_one :en,
>     :class => :ArticleContent,
>     :key => :id,
>     :primary_key => :article_id,
>     :conditions => {:language_id => 'en', :available => true},
>     :graph_join_type => :inner
>
> So far, so good. These associations work as I'd expect. Now, when I
> build a list, I do the following:
>
> articles = Article.order(:slug).
>       eager_graph(:en).
>       eager_graph(:source).
>       set_graph_aliases(
>         :id => [:articles, :id],
>         :slug => [:articles, :slug],
>         :en_title => [:en, :title],
>         :en_summary => [:en, :summary],
>         :source_slug => [:source, :slug],
>         :source_title => [:source, :title]
>       )
>
> This ALMOST works, but not quite. Here's what happens:
>
> articles[0].id -> 1
> articles[0].slug -> "article-1-slug"
> articles[0].source.id -> <proper-id>
> articles[0].source.slug -> "source-for-article-1"
> articles[0].en.title -> "Article 1 Title"
> articles[0].en.summary -> "A proper summary for Article 1."
>
> This is all correct. It's when I try any other record that I run into
> issues:
>
> articles[5].id -> 4
> articles[5].slug -> "article-4-slug"
> articles[5].source.id -> <proper-id>
> articles[5].source.slug -> "source-for-article-4"
> articles[5].en.title -> "Article 1 Title"
> articles[5].en.summary -> "A proper summary for Article 1."
>
> For some reason ALL of the .en associations are set to the first item.
> What's odd is that the SQL this produces is:
>
> SELECT "articles"."id", "articles"."slug", "en"."title" AS "en_title",
> "en"."summary" AS "en_summary", "source"."slug" AS "source_slug",
> "source"."title" AS "source_title" FROM "articles" INNER JOIN
> "article_contents" AS "en" ON (("en"."article_id" = "articles"."id")
> AND ("en"."language_id" = 'en') AND ("en"."available" IS TRUE)) INNER
> JOIN "article_sources" AS "source" ON ("source"."id" =
> "articles"."source_id") ORDER BY "slug"
>
> This SQL produces all the right values. I cannot figure out why the
> "en" association points to the wrong place. I thought at first maybe
> it was the conditions in the association, but removing those didn't
> change anything. If both of the associations were screwy, I'd have a
> better way of tracking this down. Even when I remove the source
> association entirely I get the same problems.
>
> Any idea what's up? I've tried renaming the association, removing the
> conditions and making them filters for the dataset, making it the only
> dataset used and it always results in the same issue. Does it have
> something to do with ArticleContent having a compound primary key or
> something?

If you put together a self contained example of this, I should be able
to troubleshoot it.  You probably should use a one_to_one association
for en, but I'm not sure if that is related to this issue.  With
eager_graph, it's often best to make sure that the primary key for
each associated model is returned correctly (you are doing that for
articles but not en or sources).  Again, though, I'm not sure that is
related to this issue.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to