I searched around on the net and managed to piece this scope together:

  named_scope :missing, {
    :joins => "INNER JOIN event_attendees ON event_attendees.event_id
= events.id",
    :select => "events.*, count(event_attendees.id) as c",
    :group => "event_attendees.event_id",
    :having => "c < events.attendees_needed AND event_attendees.state !
= 'available'"

  }

It seems to work like a charm.. My SQL is a bit rusty though, so it
might not be the most appropriate SQL statement.. I've tried to use a
RIGHT JOIN, but it wasn't implemented into sqlite it seems..

But the scope is not intented for a site with that meny users, so i'm
not going to work more with it..


Thanks anyway.. :)

On 5 Maj, 02:06, kevinpfromnm <[email protected]> wrote:
> don't know of a database agnostic way to do it but you can do it with
> sql.  you need to compare a count of records pointing to the event_id
> to the value in the event row.  my sql is too rusty to do from memory,
> might be GROUP or might be straight up under COUNT()
>
> On May 4, 6:20 am, Mikkel WF <[email protected]> wrote:
>
>
>
> > Hi guys..
>
> > I have a problem with a scope for one of my models.
> > The models a roughly like this:
>
> > class Event
> >         fields do
> >                 attendees_needed  :integer
> >         end
> >         has_many :event_attendees, :dependent => :destroy
> >         has_many :attendees, :through => :event_attendees, :accessible =>
> > true
> > end
>
> > class EventAttendee
> >   belongs_to :event
> >   belongs_to :user
>
> >   lifecycle do
> >                 state :available, :volunteer, :manager
> >         end
> >         named_scope :attending, :conditions => { :state => ["volunteer",
> > "manager"], }
> > end
>
> > I need to scope the events for which that does not have sufficient
> > attendees. I've made a scope in EventAttendee that returns the
> > attendees that are actually attending, so i need to compare this count
> > with the attendees_needed integer, all resulting in a scope for the
> > Event model.
>
> > But i cannot get my head around doing that, so help is very much
> > appreciated..
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Hobo Users" 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 
> > athttp://groups.google.com/group/hobousers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Hobo Users" 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 
> athttp://groups.google.com/group/hobousers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" 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/hobousers?hl=en.

Reply via email to