Why not have a:

class FeaturedProject < ActiveRecord::Base
   belongs_to :project

   before_create :only_have_one
   validates_associated :project

   def only_have_one
     self.class.count < 1
   end
end

Then you can FeaturedProduct.find(:first) and be sure that there is  
only one. No need to mess with the projects when the featured one  
changes.  However, you might want to do:

class Project
   after_destroy :clean_up_if_featured

   def clean_up_if_featured
     fp = FeaturedProject.find(:first)
     if fp && fp.project_id == self.id
       fp.destroy
     else
       true
     end
   end
end

Although that might be equivalent to:

class Project
   has_one :featured_project, :dependent => :destroy
end

-Rob

On Feb 18, 2009, at 12:43 PM, yaphi wrote:
> Hey Maurício,
>
> I have a project model. What I'd like to do is set a project to
> "featured" so I can display that on the homepage. By marking a project
> as featured, I'd want all the other projects to automatically have
> their "featured" column set to false.
>
> On Feb 17, 8:55 pm, Maurício Linhares <[email protected]>
> wrote:
>> No, there isn't. Maybe you're approaching the problem from the wrong
>> point of view.
>>
>> Try to explain what is your problem that someone else might give  
>> you a
>> better idea.
>>
>> -
>> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) 
>> |http://blog.codevader.com/(en)
>>
>> On Tue, Feb 17, 2009 at 10:13 PM, yaphi <[email protected]> wrote:
>>
>>> I'm not sure if there is a term for this (which is why I can't find
>>> anything on google) but I want to be able to set one of my models
>>> active, where the rest will be set to inactive.
>>
>>> I would guess to write a method that sets all the records to  
>>> inactive,
>>> then set the selected object to active. That seems like it's pretty
>>> messy though. Is there some sort of built-in functionality with  
>>> rails
>>> that will only allow one column to be true at a time?

Rob Biedenharn          http://agileconsultingllc.com
[email protected]



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to