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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:34:47+00:00 2026-05-27T19:34:47+00:00

Hi I am new to rails and started with a simple app which has

  • 0

Hi I am new to rails and started with a simple app which has many ‘tasks’ and each one have a ‘tag’ attached with it. So the relationship is like – Many ‘tasks’ have one/same ‘tag’. How do I bring this up in my models. I tried it with ‘task’ has_one ‘tag’ and a ‘tag’ belongs_to a ‘task’ rule but it works only for the first ‘task’ with that tag and for the rest of the ‘tasks’ with the same ‘tag’ it won’t work. Please suggest me the way to do this properly. Thanks 🙂

class Task < ActiveRecord::Base
  has_many :logs
  has_one :tag , :foreign_key => "id"
end

class Tag < ActiveRecord::Base
  belongs_to :task
end
  • 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-27T19:34:48+00:00Added an answer on May 27, 2026 at 7:34 pm

    It looks to me like you have the associations turned around – every newbie to Rails that I’ve ever seen has done this on their first project.

    What you want is

    class Task < ActiveRecord::Base
      has_many :logs
      belongs_to :tag
    end
    
    class Tag < ActiveRecord::Base
      has_many :tasks
    end
    

    Think about it this way – if you click on a tag, you would expect a list of all tasks that have that tag. So tags have many tasks, and each task belongs to one tag. This matches how you described your project. Has_one is intended for a one to one relationship, where each task would have its own, unique tag.

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

Sidebar

Related Questions

I have started developing a new Rails app on my server using RVM, Rails
I am working on a new rails 3.2 company management app which relies heavily
SOLUTION FOUND: See comment Building a new rails 3.1 app. Started with a basic
I'm new to Rails and have started a project that I'm unhappy with my
I am co-developing a simple web app in Rails 3.0.9 and I have realized
I am new to ruby on rails and started a tutorial and found out
Started working on a new application this week that is running the latest rails
Starting a new rails project and we have a well-thought-out color palette, and want
I'm developing a new rails application which is supposed to be installed several times
I just created a new rails app in Rails 3.1.1, and my application layout

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.