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 8723299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:35:54+00:00 2026-06-13T07:35:54+00:00

I have a Post model with a draft column. If the user click on

  • 0

I have a Post model with a draft column. If the user click on the checkbox in the view, the post is checked as draft

schema.rb:

create_table "posts", :force => true do |t|
    t.string   "title"
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at",                            :null => false
    t.datetime "updated_at",                            :null => false
    t.integer  "comments_count",     :default => 0,     :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.boolean  "published",          :default => false
    t.datetime "published_at"
    t.boolean  "draft",              :default => false
  end

In other words, it is not considered published:

post.rb:

  def publish_post
    unless self.draft == true
     self.published = true
     self.published_at = Time.now
    end
  end

(I’m not sure if it’s a bad practice to have two columns that mean exactly the opposite of each other, but it makes the callback in the ‘Post’ model read clearer. Please, correct me if I’m wrong).

Now I want to find a way of fetching draft and non-draft (published) posts. Right now, I only know how to sort them by their published date. But I don’t know how to filter out the posts with draft == true or published == false.

posts_controller.rb:

 def index
    @posts = Posts.order('published_at DESC')
  end

What’s the best way of doing this?

(By the way, should I use order or order_by? What’s the difference?)

  • 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-06-13T07:35:55+00:00Added an answer on June 13, 2026 at 7:35 am

    You can also do this with an ActiveRecord scope, which gives you a nicer way to query all draft posts:

    class Post < ActiveRecord::Base
       #Create a scope for draft
       scope :draft, where(:draft => true)
    end
    

    Then, in your controller – you can find all the draft posts like this:

    Post.draft
    

    You can read more on scopes here
    http://guides.rubyonrails.org/active_record_querying.html#scopes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Post model: create_table posts, :force => true do |t| t.string
I have a Post model: create_table posts, :force => true do |t| t.string title
I have a method in my Post model which gets the user posts def
I have post and user model and I have Many( posts ) to One(
I have a Topic model and a Post model. A Topic has many Posts,
Let's say I have User model and Post model. Post model contains field user_id
I have a User model, a Post model, and an Interest model. User has_many
I have a model Post : class Post < ActiveRecord::Base has_one :draft, class_name: Post,
I have a Post model: class Post < ActiveRecord::Base belongs_to :user has_many :taggings, :dependent
I have a Post model and a View model (which logs the view of

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.