[Rails] Re: named scope for 'all without conditional associations'
On Mon, Oct 12, 2009 at 3:02 PM, Max Williams
<rails-mailing-list@andreas-s.net> wrote:
>
> That title doesn't explain it very well...
>
> Basically a User has many sign_ins, and sign_ins have a created_at
> field.
>
> I want to do a named scope on User which returns all users who have no
> sign_ins in the last two months (ie have no associated sign_in records
> where created_at >= 2.months.ago). This feels like it should be simple
> but i can't get it.
>
If you want to make a named_scope, I guess you will need joins with
the sign_ins table.
something like "INNER JOIN intermediate_table ON
intermediate_table.user_id = users.id INNER JOIN sign_ins ON
sign_ins.id = intermediate_table.sign_in_id WHERE
DATETIME(sign_is.created_at) > DATETIME(#{named_scope_parameter})"
(this is just a quick first approach, you probaly have to re-think it
and adapt it to your database).
Other approach can be create a named scope in the sign_ins class, to
retrieve all the user ids that have no signs in, and then create a
named scope or a method on User to filter the results based on these
ids.
Hope it helps.
--
Leonardo Mateo.
There's no place like ~
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home