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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:42:15+00:00 2026-06-04T21:42:15+00:00

I currently have a tagging system in place with the following code rails g

  • 0

I currently have a tagging system in place with the following code

 rails g model tag name:string
 rails g model tagging post_id:integer tag_id:integer
 rake db:migrate

  class Tagging < ActiveRecord::Base
    belongs_to :post
    belongs_to :tag
  end

  class Tag < ActiveRecord::Base
    has_many :taggings, :dependent => :destroy
    has_many :posts, :through => :taggings
  end

  class Post < ActiveRecord::Base
    has_many :comments, :dependent => :destroy
    has_many :taggings, :dependent => :destroy
    has_many :tags, :through => :taggings
    validates_presence_of :name, :content
    attr_writer :tag_names
    after_save :assign_tags

    def tag_names
      @tag_names || tags.map(&:name).join(' ')
    end

    private

    def assign_tags
      if @tag_names
        self.tags = @tag_names.split(/\s+/).map do |name|
          Tag.find_or_create_by_name(name)
        end
      end
    end
  end

  <p>
    <%= f.label :tag_names %><br />
    <%= f.text_field :tag_names %>
  </p>

in the _post.html.erb partial

I am trying to call a post with a specific tag by doing

 <%= post.find_by_tag("sports") %>

When I do this all the posts with the tag come up which is what I want but they come up in this format

 [#<Post id: 11, title: "adfads", body: "adsfasd", user_id: 1, created_at: "2012-05-30 06:17:52", updated_at: "2012-05-30 06:17:52", votes_count: 1>, #<Post id: 8, title: "Cover Letter", body: "Dear Human Resources:\r\n\r\nI would like to make mysel...", user_id: 1, created_at: "2012-05-30 00:28:56", updated_at: "2012-05-30 00:28:56", votes_count: 1>]

How can I make them come up in a more agreeable fashion of post title post body instead of what it is rendering right now?

  • 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-04T21:42:16+00:00Added an answer on June 4, 2026 at 9:42 pm

    You should really have your find in the controller. Ignoring that issue, try this:

    <% my_posts = post.find_by_tag("sports") %>
    <% my_posts.each do |post| %>
      <%= post.title %><br />
      <%= post.body %><br />
    <% end %>
    

    Ideally, you would have @posts = post.find_by_tag("sports") in the controller, and then loop through @posts instead of my_posts. Format the above as needed.

    Hope this helps.

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

Sidebar

Related Questions

Currently I have the following code for a project that represents some probability trees
Heres what I'm trying to accomplish: I have a tagging system in place. Tags
I have the following query method in my ActiveRecord model: def self.tagged_with( string )
I currently have the following that will search a field within my db for
Currently have the following expression: @[TMS_Dest] + \\ + @[TMS_Rename] + (DT_WSTR,4)DatePart(yyyy, GetDate()) +
So I am currently implementing a new tagging system for on top of an
I have three tables, bookmarks , tag and tagging . tagging is the association
Working in a Rails App, I have the following table structure (pertinent columns only)
The Idea is to have a tagging system between Users and Content(images, videos, posts)
Currently have a custom Form Element View: class Apply_View_Helper_JQueryUiFormRadio extends Zend_View_Helper_FormElement My form is

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.