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? --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Tue, Feb 17, 2009 at 10:13 PM, yaphi <jcontonio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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?--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
<mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> 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
<jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Interesting...So I''d have a method in my Project model that sets the FeaturedProject. I''ll play with this and see how it goes. Thanks! On Feb 18, 2:08 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> 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 <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > 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 <jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, I don''t know if a Project makes itself featured, but
that''s your
dilemma.
In any case, it would be something that a controller action calls on
either a Project instance (@project.feature_me) or the FeaturedProduct
model (FeaturedProject.is_now(@project)).
Where you might have:
class FeaturedProject
def self.is_now(a_project)
fp = find(:first) || new
fp.project = a_project
fp.save
end
end
-Rob
On Feb 18, 2009, at 4:16 PM, yaphi wrote:
>
> Interesting...So I''d have a method in my Project model that sets
the
> FeaturedProject.
>
> I''ll play with this and see how it goes. Thanks!
>
> On Feb 18, 2:08 pm, Rob Biedenharn
<R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>
> wrote:
>> 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
<mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
>>> 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
<jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
>>>> 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
>> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
> >
Rob Biedenharn http://agileconsultingllc.com
Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I''ve been thinking more about this, and instead of doing a whole new model, couldn''t I do something like this in my Project model? before_save :set_if_featured def set_if_featured articles = Article.find(:first, :conditions => ["featured = ?", true]) article.featured = false featured = true end On Feb 18, 4:49 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> Well, I don''t know if a Project makes itself featured, but that''s your > dilemma. > > In any case, it would be something that a controller action calls on > either a Project instance (@project.feature_me) or the FeaturedProduct > model (FeaturedProject.is_now(@project)). > > Where you might have: > > class FeaturedProject > def self.is_now(a_project) > fp = find(:first) || new > fp.project = a_project > fp.save > end > end > > -Rob > > On Feb 18, 2009, at 4:16 PM, yaphi wrote: > > > > > > > Interesting...So I''d have a method in my Project model that sets the > > FeaturedProject. > > > I''ll play with this and see how it goes. Thanks! > > > On Feb 18, 2:08 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > > wrote: > >> 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 <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>> 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 <jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>>> 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 > >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
How about a class method that takes the to-become-featured project as an
argument? Something like:
class Project < AR:Base
validates_uniqueness_of :featured
def self.make_featured(this_project)
self.update_all("featured = false")
this_project.featured = true
this_project.save!
end
end
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk@googlegroups.com] On Behalf Of yaphi
Sent: Thursday, February 19, 2009 4:37 AM
To: Ruby on Rails: Talk
Subject: [Rails] Re: One model active at a time
I''ve been thinking more about this, and instead of doing a whole new
model, couldn''t I do something like this in my Project model?
before_save :set_if_featured
def set_if_featured
articles = Article.find(:first, :conditions => ["featured = ?",
true])
article.featured = false
featured = true
end
On Feb 18, 4:49 pm, Rob Biedenharn
<R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>
wrote:> Well, I don''t know if a Project makes itself featured, but
that''s your
> dilemma.
>
> In any case, it would be something that a controller action calls on
> either a Project instance (@project.feature_me) or the FeaturedProduct
> model (FeaturedProject.is_now(@project)).
>
> Where you might have:
>
> class FeaturedProject
> def self.is_now(a_project)
> fp = find(:first) || new
> fp.project = a_project
> fp.save
> end
> end
>
> -Rob
>
> On Feb 18, 2009, at 4:16 PM, yaphi wrote:
>
>
>
>
>
> > Interesting...So I''d have a method in my Project model that
sets the
> > FeaturedProject.
>
> > I''ll play with this and see how it goes. Thanks!
>
> > On Feb 18, 2:08 pm, Rob Biedenharn
<R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>
> > wrote:
> >> 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
> >>>
<mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> >>> 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
<jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> >>>> 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
> >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
>
> Rob Biedenharn http://agileconsultingllc.com
> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
that''s exactly what I want...didn''t know about update_all! Thanks I''ll try it when I get back tonight. On Feb 19, 3:33 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> How about a class method that takes the to-become-featured project as an argument? Something like: > > class Project < AR:Base > validates_uniqueness_of :featured > > def self.make_featured(this_project) > self.update_all("featured = false") > this_project.featured = true > this_project.save! > end > end > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of yaphi > Sent: Thursday, February 19, 2009 4:37 AM > To: Ruby on Rails: Talk > Subject: [Rails] Re: One model active at a time > > I''ve been thinking more about this, and instead of doing a whole new model, couldn''t I do something like this in my Project model? > > before_save :set_if_featured > > def set_if_featured > articles = Article.find(:first, :conditions => ["featured = ?", > true]) > article.featured = false > featured = true > end > > On Feb 18, 4:49 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > wrote: > > Well, I don''t know if a Project makes itself featured, but that''s your > > dilemma. > > > In any case, it would be something that a controller action calls on > > either a Project instance (@project.feature_me) or the FeaturedProduct > > model (FeaturedProject.is_now(@project)). > > > Where you might have: > > > class FeaturedProject > > def self.is_now(a_project) > > fp = find(:first) || new > > fp.project = a_project > > fp.save > > end > > end > > > -Rob > > > On Feb 18, 2009, at 4:16 PM, yaphi wrote: > > > > Interesting...So I''d have a method in my Project model that sets the > > > FeaturedProject. > > > > I''ll play with this and see how it goes. Thanks! > > > > On Feb 18, 2:08 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > > > wrote: > > >> 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 > > >>> <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > >>> 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 <jconto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > >>>> 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 > > >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > Rob Biedenharn http://agileconsultingllc.com > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
yaphi wrote:> that''s exactly what I want...didn''t know about update_all! Thanks I''ll > try it when I get back tonight.That sucks. You can only have one featured row in your table. Why would you identify this row by marking all your rows one way or another (you will need to index this column (at some point) also) when all you need to store (somewhere) is the id of the current featured row ? This is an heavyweight solution to a flyweight problem. Create a control/settings table/model that has an attribute "current_featured_project_id" and access it via that (as shown already). You might find other single settings values etc.. that you can add to this table later. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
OK I see what you''re saying. That''s much simpler and more future proof. Thanks again! On Feb 20, 11:43 am, Andrew Porter <a...-OV3k8GMR8cdg9hUCZPvPmw@public.gmane.org> wrote:> yaphi wrote: > > that''s exactly what I want...didn''t know about update_all! Thanks I''ll > > try it when I get back tonight. > > That sucks. > > You can only have one featured row in your table. > > Why would you identify this row by marking all your rows one way or > another (you will need to index this column (at some point) also) when > all you need to store (somewhere) is the id of the current featured row > ? This is an heavyweight solution to a flyweight problem. > > Create a control/settings table/model that has an attribute > "current_featured_project_id" and access it via that (as shown already). > You might find other single settings values etc.. that you can add to > this table later.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---