Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 451253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:57:01+00:00 2026-05-12T21:57:01+00:00

this is a really stupid question but I have a deadline and I’m very

  • 0

this is a really stupid question but I have a deadline and I’m very nervous and I’ve tried everything. In fact it even worked for a second but I must have broken something.

I have a default featured item that I want to be overridden any time I try to feature another item. I had it give an error before but I don’t want it to error, I just want it to set everything to false and this app to true.

I’ve tried everything, before save, after save/update named scopes, mapping each one to false and then saving, checking for whether the feature has changed…I think I have all the right parts but I can’t put them together. Please please help 🙁

Maybe there’s even a quick validation for this, I’m not sure

I can’t elaborate with code because I’ve tried a million different things.

There is a boolean setting on an item that is called is_featured.

When you create the item, you can set it to be featured or not.

In the database, if other items are featured, these should be zeroed out, and the item that was just updated or created to be featured should be true.

I have tried this in the model with before/after/save/update and also the controler on create and update

Fine I will add some of my stupid code which Id on’t know why doesn’t work

In the controller:

  if params[:app][:is_on_front_page] == true
App.all.map{|m|( m != @app ) ? m.is_on_front_page = true : m.is_on_front_page = false}

end

or

  named_scope :is_on_front_page, :conditions => { :is_on_front_page => true }
    after_update do
    if self.is_on_front_page.changed? and App.is_on_front_page.count >= 1
     App.all.map{|m| m.is_on_front_page = false;m.save}

    end 
    is_on_front_page = true
    save!
   end

but it won’t let me save or gets into an infinite loop or yells at me because of the named scope

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:57:01+00:00Added an answer on May 12, 2026 at 9:57 pm

    In a transaction just set all to false and then update the record you want to true (featured). This is similar to how sometimes its easier to delete records then find them and update them. That is, its easier to just do something like:

    tranasction do
    SomeModel.delete_all
    items.each { |i| SomeModel.create(i) }
    end

    Which can be easier then looping over SomeModel doing a find() and then updating certain properties.

    In your case you just want to mark ALL records a NOT-featured and then just update the one you want as featured, rather than toggling records by hand.

    Try something like this:

    after_create :set_as_featured
    
    def set_as_featured
     self.class.transaction do
       self.connection.execute("UPDATE foo SET featured=0")
       self.update_attribute(:featured, true)
     end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this may be a stupid question to ask but I have really
This might be a really stupid and obvious question, but I'm very new to
This is probably a really stupid MSBuild question but if I have <ItemGroup> <Dll
This is a really stupid question, but I need a little reassurance/assistance. I have
I'm really sorry. This must seem like an incredibly stupid question, but unless I
This is probably a really stupid question but I can't figure this out. I
So, this may be a really stupid question, but I'm obviously missing something here.
Ok. This is probably a really stupid question, but I'm going to ask it
ok... so I feel really stupid asking this question.. but just wondering about the
This might be a stupid question but i would really appreciate any compact answer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.