Thanks for the help Jeremy-- I'm always amazed at how rapidly and
thoroughly you provide support.
On Wednesday, February 20, 2013 6:01:22 PM UTC-5, Jeremy Evans wrote:
>
> On Wednesday, February 20, 2013 8:54:18 AM UTC-8, Sean Mackesey wrote:
>>
>> I have a schema with three tables: *Number*, *Article*, and *
>> ArticleNumber*. *ArticleNumber* is a join table. Some records in *
>> Number* are associated with *Article* records via the join table, and
>> some are not. I have defined a *many_to_many *association that allows
>> me to filter by *Number*s having one of a particular set of *Articles.
>> *But,
>> short of writing raw SQL or doing *Number.all.select {...}*, how do I
>> filter for *Number* records that have no corresponding article? Neither
>> of these work:
>>
>> *Number.filter(Article: nil)*
>> * Number.filter(Article: [])*
>> *
>> *
>> Thanks.
>>
>
> You have a couple of errors in your code. First, you are using Article:
> when you probably should be using articles: (you must match the association
> name, which is generally lowercase and plural for many_to_many). Also, you
> can't use filter in this case, since you are looking for something that
> doesn't match. You need to use exclude. Assuming you want all Numbers
> that have no corresponding article using the filtering by associations
> support, you can probably do:
>
> Number.exclude(:articles => Article.dataset)
>
> However, the query that uses is probably suboptimal for your case. You
> probably can make do with just the Number instances that have no matching
> foreign key in the join table:
>
> Number.exclude(:id=>ArticleNumber.select(:number_id))
>
> 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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.