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

  • Home
  • SEARCH
  • 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 3874092
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:06:45+00:00 2026-05-19T22:06:45+00:00

I have a rather complicated (to me) statements to write in Rails. Under the

  • 0

I have a rather complicated (to me) statements to write in Rails. Under the show.html.erb of my Person.rb which is a user’s profile. Each post has a status column in database with values draft or published.

<% if @posts.blank? %>
  No post.
<% else %>
  <% if @person == current_user %>
    <% @posts.each do |post| %>
      Post title... (where all published and draft posts are shown)
    <% end %>
  <% else %>
    <% @posts.each do |post| %>
      <% if post.status == "published" %>
        Post title (where only published posts are shown, draft posts are hide from other users)
      <% end %>
    <% end %>
  <% end %>
<% end %>

Here’s the problem. Suppose User A has 1 published post and 1 draft post, it works fine; but if User B has 2 draft posts, by right no post will be shown to other users, but I want it to show No post like the first 2 lines code.

I tried named_scope in my Post.rb guided here, but when I apply .published it just returned me a non-defined error.

Please teach me how to accomplish this. Thank you very much.

  • 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-19T22:06:45+00:00Added an answer on May 19, 2026 at 10:06 pm

    I would do the following:

    Create a scope for status on posts.

    class Post
      named_scope :by_status, lambda { |status| {:conditions => {:status => status} } }
    end
    

    Move some logic into your controller:

    class PostsController
      def index
        @person = Person.find(params[:person_id])
        if current_user == @person
          @posts = @person.posts
        else
          @posts = @person.posts.by_status('published')
        end
      end
    end
    

    Write your view:

    <% if @posts.empty? %>
      No posts code
    <% else %>
      &lt% @posts.each do |post| %>
        Post title...
      <% end %>
    <% end %>
    

    This solution should DRY up your code and move more logic into your model.

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

Sidebar

Related Questions

I have the following tricky problem: I have implemented a (rather complicated) class which
I have been using django to write a rather complicated object-oriented model for a
I have a rather complicated setup which I have boiled down to the code
My setup: Rails 2.3.10, Ruby 1.8.7 I have a rather complicated set of relationships
I have a rather complicated function the I would like to write as an
I have a rather complicated lookup for an option. It comes from a separate
I have a rather complicated (and very inefficient) way of getting utilisation from a
I have a rather complicated deploy setup for our Drupal site that is a
I have a rather large text corpus, of which I would like to check
I have a rather enormous project in which I'm trying to retrofit in-memory data.

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.