How to choose a single model and persist that choice?

I have a simple model called Party with a corresponding table called parties. There's also a controller with all the usual CRUD actions and so on. This model is used in a website and only one admin user is allowed to edit the parties - everyone else is allowed to call GET actions (index, show). Nothing special so far.

Now I need to do the following: The admin would like to choose a single Party at a time for special presentation (the selected Party is showing up on the start page of the application). The most important thing is, that there's only ONE party at time selected.

How would you solve this problem? Boolean Flag in Party model? Save the selection (id of the party) somewhere outside the database? Implement a new model with a has_one relation to Party (seems like overkill to me)?

I hope my explanation is good enough to understand the issue.

Answers


A simple "front_page" attribute would suffice or another model like you mentioned, using the has_one relationship would be fine as well.

Using another model would allow you to maintain some more information, like how long should it remain on the front page (expiration date?) or how many times it was featured (assuming a party can be featured twice). It really depends on other requirements for your system.

You also might be able to get away with a simple implementation of the Singleton pattern as well. There's a quick description on the Rails Wiki of making an ActiveRecord object a Singleton (see below): http://wiki.rubyonrails.org/rails/pages/TipsAndTricks

Making a singleton ActiveRecord object

If you have a table with just one entry, useful for keeping track of a number sequence for databases without sequences, you can use the singleton module included with ruby like so:

require 'singleton'

class Master < ActiveRecord::Base
  include Singleton
  def initialize(args=nil) super(args) if record = Master.find(:first)    
    self.attributes = record.attributes end end def next_tracking_number increment!
    (:current_tracking_number) current_tracking_number end def 
    self.next_tracking_number instance.next_tracking_number 
  end
end

Update:

This is a very poor code example (was copied and pasted from the Rails Wiki, which had no formatting). I would highly recommend the [Ruby Design Patterns] book which tackles many of the GoF design patterns in greater detail (while making them applicable to Ruby applications). But Google should return you some good resources for using the Singleton pattern in Ruby.[2]


Need Your Help

Django QuerySet with exclude & annotate not returning any results

python django django-queryset django-orm

Based off this SO question about counting forgein keys, I have a problem. This is my models:

Why do Labels get Type mismatch errors in VBA but ComboBoxes don't?

excel vba excel-vba

I'm running Excel 2003. I have the following VBA code, corresponding to a simple UserForm comprising a ComboBox control and a Label control. It works through the save of the ComboBox but generates ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.