On Jeff's method, one quick optimization would be to switch to using
a NOT IN query:
# in app/models/user.rb:
def not_yet_listened_recordings
ls = Listening.find_by_sql(["select distinct recording_id from
listenings where user_id=?, self.id])
return Recording.find(:all) if not ls or ls.empty?
Recording.find(:all, :conditions => ['id NOT IN (?)', ls])
end
A lot less code for the same result...
Overall though, the Fred's LEFT OUTER JOIN thing is much more elegant;
it also lends itself to being used as a named scope - which you'll
want when
it comes time to paginate the list.
Example:
# in recording.rb:
named_scope :not_listened, lambda { |u| { :joins => "LEFT OUTER JOIN
listenings ON recording_id = recordings.id AND listener_id = #
{u.id}", :conditions => [ "recording_id IS NULL" ] } }
Then you could have a controller action (using mislav-will_paginate);
def show_not_listened
# get a user object somehow - either logged in user or from params
@recordings = Recording.not_listened(user).paginate(params[:page] ||
1)
end
--Matt Jones
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---