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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:31:44+00:00 2026-06-04T22:31:44+00:00

I am new to rails and am having troubles figuring out some things with

  • 0

I am new to rails and am having troubles figuring out some things with ActiveRecord.

Right now, I have three models:

class Project < ActiveRecord::Base
    attr_accessible :name
    has_and_belongs_to_many :tags
    has_many :tasks
end

class Task < ActiveRecord::Base
    attr_accessible :todo
    has_and_belongs_to_many :tags
    has_many :tasks
end

class Tag < ActiveRecord::Base
    attr_accesible :description
    has_and_belongs_to_many :projects
    has_and_belongs_to_many :tasks
end

I am trying to create a hash that returns tasks that belong to specific tags so that:

Project_Tasks = { 1 => { project.name, "tasks" => { "task 1", "task 2", "task 3" } 
                  2 => { project.name, "tasks" => { "task 1", "task 2", "task 3" } }

I am not sure quite how to go about creating this. My first inclination is to create a method inside one of the classes (I have gone back and forth on which one… right now, I think it is best served under “tag”) that loops through the projects that match the given tag, queries for tasks that match both and append them to the array.

To date, this hasn’t worked. I’m completely stumped.

Any thoughts on how I can accomplish this? Is a method the appropriate way to go or is there a trick inside ActiveRecord to create a query that would get me at least close to 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-06-04T22:31:47+00:00Added an answer on June 4, 2026 at 10:31 pm

    I have tried to fix your model definitions.

    class Project < ActiveRecord::Base
        attr_accessible :name
        has_and_belongs_to_many :tags
        has_many :tasks
    end
    
    class Task < ActiveRecord::Base
        attr_accessible :todo
        has_and_belongs_to_many :tags
        belongs_to :project
    end
    
    class Tag < ActiveRecord::Base
        attr_accesible :description
        has_and_belongs_to_many :projects
        has_and_belongs_to_many :tasks
    end
    

    Now you should be able to access your data(in controller) for a specific project as follows:

    @project = Project.find_by_id(1)  # Loaded a project
    @tasks = @project.tasks  # all task for this project in an array
    

    To display it in the view:

    <%= @project.name %><br />
    <% @tasks.each do |task| %>
      <%= task.todo %><br />
    <% end %>
    

    I hope this helps

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

Sidebar

Related Questions

I'm new to rails and having trouble figuring out how to do this RESTfully.
I'm new to rails and I'm having some trouble in the console. I'd like
I'm brand new to ruby and rails and I'm having trouble creating a sign-out
Starting a new rails project and we have a well-thought-out color palette, and want
I'm having some troubles with my Rails app after installing (and removing) Compass. bundle
I'm having some trouble figuring out how to 1) traverse a directory and 2)
I'm having some troubles writing a Rails app that organizes scientific papers. Each paper
I am new to Ruby and Rails and am having trouble getting a class
Just got a new macbook pro and im having troubles installing rails.. I got
I am new to rails and I having trouble using cucumber to run my

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.