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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:46:27+00:00 2026-05-23T17:46:27+00:00

I have the code def showTags(post) tagString = ” tagString += Politics if post.tags(:politics)

  • 0

I have the code

def showTags(post)
    tagString = ''
    tagString += "Politics " if post.tags(:politics)
    tagString += "Technology " if post.tags(:technology)
    tagString += "Entertainment " if post.tags(:entertainment)
    tagString += "Sports " if post.tags(:sports)
    tagString += "Science " if post.tags(:science)
    tagString += "Crime " if post.tags(:crime)
    tagString += "Business " if post.tags(:business)
    tagString += "Social " if post.tags(:social)
    tagString += "Nature " if post.tags(:nature)
    tagString += "Other " if post.tags(:other)

    return tagString
end

My tags model has 10 boolean values (politics, tech, etc…) and belongs_to :post. When you make a new post there are check boxes that correspond to each field. So after making a post there are some trues and some false values. The problem arises when I call this method in the post#index, in order to display which tags belong to this post. The problem is that all of the words display, i played with the console and the post.tags is correct (some true and some false values) however if post.tags(:politics) (or whatever) always returns true. I tried using if post.tags.politics but that just gave an error.

Can someone please help me with this.

  • 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-23T17:46:28+00:00Added an answer on May 23, 2026 at 5:46 pm

    Assuming you have belongs_to :post in your Tag class and has_many :tags in your Post class then the tags attribute is a list so you either need to iterate over it:

    post.tags.each {|tag| puts tag.politics}
    

    Or index into it:

    post.tags[0].politics
    

    From an outside glance this type of model doesn’t make too much sense though. Maybe do has_one :tag in the Post class instead. Then you can call post.tag.politics.

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

Sidebar

Related Questions

If I have a block of code like this: def show @post = Post.find(params[:id])
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts

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.