Permalink
Browse files

Don't show already purchased jokes in main list

  • Loading branch information...
Martin Kleppmann
Martin Kleppmann committed Apr 14, 2009
1 parent 3c8957f commit f8a0ffd2d059f11e688501083c1c95cdc45a6b1d
@@ -5,7 +5,7 @@ class JokesController < ApplicationController
# GET /jokes
# GET /jokes.xml
def index
- @jokes = Joke.find(:all)
+ @jokes = Joke.not_purchased_by_company(current_user.company)
respond_to do |format|
format.html # index.html.erb
View
@@ -1,2 +1,11 @@
class Joke < ActiveRecord::Base
+ has_many :purchases
+
+ named_scope :not_purchased_by_company, lambda{|company| {
+ :joins => 'LEFT JOIN purchases ON purchases.joke_id = jokes.id AND ' +
+ sanitize_sql_array(['purchases.company_id = ?', company.id]),
+ :conditions => 'purchases.id IS NULL'
+ }}
+
+ validates_presence_of :question, :answer
end
@@ -1,4 +1,4 @@
-<h1>Listing jokes</h1>
+<h1>Available jokes</h1>
<ul>
@@ -13,7 +13,7 @@
<ul>
<li><%= link_to 'My jokes', {:controller => 'jokes', :action => 'my'}, {:title => "See a list of jokes you have purchased"} %></li>
- <li><%= link_to 'All jokes', jokes_path, :title => "All available jokes" %></li>
+ <li><%= link_to 'Available jokes', jokes_path, :title => "All available jokes" %></li>
</ul>
<p style="color: green"><%= flash[:notice] %></p>

0 comments on commit f8a0ffd

Please sign in to comment.