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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:40:48+00:00 2026-05-16T04:40:48+00:00

I have the following code in a layout: Posted <%=time_ago_in_words post.created_at %> ago <%

  • 0

I have the following code in a layout:

Posted <%=time_ago_in_words post.created_at %> ago 
<% if post.has_tag != nil %>
   in the <%= post.get_first_tag.name %> category
<% end %>

And the following code in the post model which is inheriting form ActiveRecord::Base

def has_tag
 !self.tags.empty?   
end

def get_first_tag
 self.tags[0]   
end 

Tags is also inherited from ActiveRecord::Base and Post ‘has_many’ Tags

Firstly: Is this the best way of checking if the post object has at least 1 associate tag attribute.

Secondly: Should I be putting this logic into a helper method?

Thirdly: Why doesn’t the following work (it returns a # where the tags should be):

in the <%= post.tags.to_sentence %> category, 

I guess its because tags aren’t actually stored as an array attribute, but i don’t really know.


  • 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-16T04:40:49+00:00Added an answer on May 16, 2026 at 4:40 am

    This is a perfectly good way of checking if there are tags or not. However, self.tags.empty? will return true or false so post.has_tag will never be nil.

    It’s worth noting that, in ruby, it is common to name methods that return true or false with a question mark. So post.has_tag? would be a better name for your method (like the empty? method for the tags).

    This sort of method belongs in the model class rather than a helper as it is not specific to the view layer; you might want to call this method from other model classes, for example.

    The reason you are getting # instead of your tag names is that you are trying to convert a collection of tags to a sentence and you need instead to convert the names of the tags to a sentence. You should be able to do

    post.tags.map(&:name).to_sentence
    

    which will take the names of the tags and turn them into a sentence.

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

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have a table layout with the following code <table cellspacing=0 class=photogalleryTable> <tbody> <tr>
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following css code for layout. td img {display: block;} While displaying images
I have the following code in my layout file: <% @families = ProductFamily.find(:all, :include
I have the following sample template code. {# @T('core.layout:Logged in as {0} {1}', @user('firstname'),
I have the following code layout in mxml. The button will be positioned correctly
I have the following code: $.post('block_ajax.php' , { 'action': 'set_layout' , 'listid': 123 ,
I have the following code: def show @game = $site.new_game_of_type(params[:id]) @game.start end def update_game_time
I have the following code to layout the menu for my page: //Header Conainer

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.