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

  • Home
  • SEARCH
  • 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 6534193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:13:17+00:00 2026-05-25T10:13:17+00:00

Models class Project has_many :tasks class Task belongs_to :project Task has the following attributes

  • 0

Models

class Project
 has_many :tasks

class Task
 belongs_to :project

Task has the following attributes (among others)

t.string project
t.integer project_id

This is code I inherited and I’m not sure why it has both columns but I noticed an unexpected behavior with this setup. When I render JSON for @task, it included the project info as such (may not be properly formatted JSON but you get the idea)

{
  "task": {
    "duration": 3,
    "project": {
      "project": {
        "id": 9,
        "description": "Roofing,
        "updated_at": "2011-09-07T16:58:34Z",
        ...
      }
    },
    "project_id": 9,
    ...
  }
}

I checked project column in the database and it’s nil. Seems like Rails treated that column like a relation call (I can see why even) instead of just a column, is that intended behavior?

  • 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-25T10:13:17+00:00Added an answer on May 25, 2026 at 10:13 am

    I think you are correct in your assumption of a naming collision. You can actually access your task’s project like that (task.project), so when the rails JSON renderer renders task.project it pulls down the JSON rendition of its relation like you’re speculating. I recommend you change the name of that column to not clash with rails’ convention, or you can change the name of the relation if you would rather not change the database column (look at the :class_name option), but it may cause more confusion down the road. I just read that you inherited that code. Usually when you specify model relations, you automatically get attributes such as project_id, and a helper method project which accesses the task’s project. Perhaps the author of the code was not aware of this and felt the need to create those two columns himself. It might be okay to keep project_id, but the project attribute is clearly clashing.

    Alternatively, you can override the JSON rendition yourself by defining a method in your model with the signature def as_json(options={}) which returns a hash representing your desired JSON rendition, ex. { :name => task.name, :project => something_else }.

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

Sidebar

Related Questions

I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
My setup: Rails 3.0.9, Ruby 1.9.2 Here are my models class Project has_many :tasks
I have the following models: require 'books_projects.rb' class Project < ActiveRecord::Base has_many :book_to_projects has_many
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end
I have a Project model which accepts nested attributes for Task. class Project <
Say I have these models class Project < ActiveRecord::Base has_many :comments end class Comment
I have a has_many and belongs_to association set up between two models: Project and
I have the following three models: User, Project, and Assignment. A User has_many Projects

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.