Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Timestamps with has_and_belongs_to_many

#3699

I'm using Rails 2.3.5.

All of my tables include the default timestamps (created_at and updated_at) including my join tables for has_and_belongs_to_many (habtm) associations. When I retrieve the habtm objects using the convention task.colors, Rails returns the timestamps from the join table instead of the color table. Since it returns Color objects, the timestamps should be from the colors table since that reflects when the Color object was last updated.

Both tables have timestamps, is Rails confusing the two? Or is this by design?

class Task < ActiveRecord::Base
  has_and_belongs_to_many :colors
end
class CreateColorsTasks < ActiveRecord::Migration
  def self.up
    create_table :colors_tasks, :id => false do |t|
      t.integer 'color_id', :null => false
      t.integer 'task_id', :null => false
      t.timestamps
class CreateTasks < ActiveRecord::Migration
  def self.up
    create_table :tasks do |t|
      t.string 'name'
      t.timestamps
class CreateColors < ActiveRecord::Migration
  def self.up
    create_table :colors do |t|
      t.string 'name'
      t.timestamps

Reported by Lyle · January 15th, 2010 @ 06:47 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ryan Bigg
    Ryan Bigg
    • State changed from new to wontfix

    Use a has_many :through if you want more than just the join columns in the join table.

    June 12th, 2010 @ 03:14 AM