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.

has_one through incorrect id add

#6251

class Device < ActiveRecord::Base
has_many :units has_many :components, :through => :units end

class Component < ActiveRecord::Base
has_one :unit has_one :device, :through => :unit end

class Unit < ActiveRecord::Base
belongs_to :device belongs_to :component end

Everything I add a device_id to a component, it adds 1 to the device_id stored in the Units table.

x=Component.new
x.device=1
x.save

device_id=2 in the Unit model

Reported by ronin-131274 (at lighthouseapp) · January 5th, 2011 @ 05:15 AM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. glebm
  2. gerardc
    gerardc

    Why would you add 1 as the device for x and not a Component object?

    January 11th, 2011 @ 02:10 PM

  3. gerardc
    gerardc

    Edit to my previous comment:

    Why would you set x.device to hold the value 1 instead of a Device object

    /newb

    January 11th, 2011 @ 02:15 PM

  4. ronin-131274 (at lighthouseapp)
    ronin-131274 (at lighthouseapp)

    If I wanted to assign a component to a device.

    January 11th, 2011 @ 02:23 PM

  5. Dan Pickett
    Dan Pickett

    this looks as though it doesn't honor the contract in associations.

    x.device = 1 is not a proper assignment. You must assign x's device association to a device object.

    x.device_id = 1 should successfully assign the foreign key for you.

    March 12th, 2011 @ 04:19 PM