0

So basically I have this for my index page:

def index
  @articles = Article.paginate(:per_page => 15, :page => params[:page])
end

I implemented soft deletion, which basically works like this: there is an additional boolean column in db that is called is_active and is true by default. Deleting is rewritten to just change that to false instead of destruction, and made a page to view soft-deleted entries.

The issue: current workaround I found for paginate is to simply add

<% if article.is_active %>

in my index.html.erb. The flaw is: when I delete something it still considered there by paginate, so instead of say 15 entries I will see 14. Even worse, on undeleting page it shows same amount of blank pages, and deleted entries are on their would-be-appropriate pages (so for example first entry may end up being on page 14 instead of 1). It is not critical flaw, but I'd like to know if I can fix it without rewriting too much.

Maybe I can change something in controller so it doesn't send any entries that have true or false in that field depending on what I want to output?

2 Answers 2

2

Can't you just filter by this field?

@articles = Article.where(is_active: true).paginate(per_page: 15, page: params[:page])
Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm! Thanks! I'm just getting hang of ruby on rails, but not completely new to programming, so I suspected there is more than just .all or .paginate, etc., just wasn't sure how to google it properly.
guides.rubyonrails.org/active_record_querying.html This guide is the best place to start, good luck
0

You can use a scope or a where clause to get only active articles:

def index
  @articles = Article.where("is_active = ?", true).paginate(:per_page => 15, :page => params[:page])
end

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.