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

The Archive Base Latest Questions

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

My Post model has a boolean called published: schema.rb: * create_table posts, :force =>

  • 0

My Post model has a boolean called “published”:

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.boolean  "published",          :default => false
  end

If it is true, the post is considered published, and it appears in the published section of the user’s profile, and if it is false it appears in the draft section (sorry, but I’m not sure how to deal with the code duplication).

posts_controller.rb:

class PostsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]

  def index
    @posts = Post.all
  end

etc…

users_controller.rb:

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @posts = @user.posts
  end

etc…

_shared/posts.thml:

   <h2>Published</h2>
    <% @posts.each do |post| %>
      <% if post.published? %>
        <div id="post-<%= post.id %>" class="post">
          <%= image_tag post.image.url(:medium) %>
          <h2 class="post-title"><%= link_to post.title, post %></h2>
          <%= link_to post.user.email, post.user %>
          <p><%= post.content %></p>
        </div>
      <% end %>
    <% end %>

    <h2>Draft</h2>
    <% @posts.each do |post| %>
      <% if not post.published? %>
        <div id="post-<%= post.id %>" class="post">
          <%= image_tag post.image.url(:medium) %>
          <h2 class="post-title"><%= link_to post.title, post %></h2>
          <%= link_to post.user.email, post.user %>
          <p><%= post.content %></p>
        </div>
      <% end %>
    <% end %>

Example:

users.html.erb:

<h2>User Show</h2>
<%= image_tag @user.avatar.url(:medium) %>
<span>Email: <%= @user.email %></span><br />
<span>ID: <%= @user.id %></span><br />

<%= render 'shared/posts' %>

index.html.erb

<%= render 'shared/posts' %>

(I know the views are a little bit messy. I think later on I will just show a text saying ‘draft’ beside each post draft).

The problem is that the posts will be sorted by their created_at date. I want them to be sorted by a published_at date in the post’s index page (I think that makes more sense) .

I know how to add the published_at t.datetime field via migration. But I’m not sure what code to use for the logic in the published_at field.

Any suggestions?

(by the way, what sounds more correct? ‘published_at‘ or ‘published_on‘?)

  • 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:14:29+00:00Added an answer on June 13, 2026 at 7:14 am

    add in your table published_at (datetime) field. and when you publish then update the published_at field and then when you fetch data use order('published_at Desc')

    @posts =  @users.posts.order('published_at DESC')
    

    logic like in your controller

    if params[:post][:published] == true # whatever you got in controller rather than params[:post][:published]
       @post.published_at = Time.now
    else
      @post.published_at = ""
    end
    

    that means, when published value is true then above code perform in your controller and in your published_at field add value Time.now and if value of published is false then above code is not perform so published_at field have blank value.

    or, by using model like

    before_save :published_post
    
    def published_post
      if self.published == true
       self.published_at = Time.now
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Topic model and a Post model. A Topic has many Posts,
I have an array of posts called @posts. Post model has_many :feelings :through =>
I have two models: Post and Tag. The Post model has an attr_accessor called
My app has a user model and a post model, where user has_many posts
I have a post model that has many tags through taggings. let's say: @posts
I have two models, post and comment. Posts has many comments and comments belongs
My Post model in schema.yml (Symfony 1.4) looks like this: Post: columns: id: {
Assuming i have a post model with created_at, title, and id. How can i
I have a post controller that has many comments. The post model has a
I have a model which has a boolean property and some other properties that

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.