0

I have action like this to add one ContestEntry record to showcase.

@entry = ContestEntry.find(params[:content_id])
if @entry.view_in_showcase == true
  @entry.view_in_showcase = false
  @entry.save
  redirect_to :back, :notice => "Drop design to showcase"
else
  @entry.view_in_showcase = true
  @entry.save
  redirect_to :back, :notice => "Add design to showcase"
end

But now I have to implement it for multiple records and I have array that includes ContestEntry records.How can I use this array with these code ?

for instance :

arr_showcase = [ContestEntry.first,ContestEntry.last,..]

How can update this array with same purpose above the text.

1
  • What exactly is the problem here? Commented Jan 29, 2013 at 17:48

2 Answers 2

1

Assuming your array is @entries :

@entries.each {|e| e.update_attribute('view_in_showcase', false) if e.view_in_showcase } 
Sign up to request clarification or add additional context in comments.

Comments

1

First step to reuse this code is to refactor it inside a method.

class ContestEntry
  def toggle_view_on_showcase!
    view_on_showcase.toggle!
    save
    view_on_showcase
  end
end

# Refactor controller
if @entry.toggle_view_on_showcase!
  redirect_to :back, :notice => "Add design to showcase"
else
  redirect_to :back, :notice => "Drop design to showcase"
end

# reuse!
arr_showcase.each { |e| e.toggle_view_on_showcase! }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.