STI and fixtures

Hi all !

Loading fixtures doens’t seems to associate my 2 objects, do know what’s
wrong ?

In app/

class Manager < Employee

has_many :employees,
:foreign_key => :reports_to

end

class Employee < Person

belongs_to :manager,
:foreign_key => ‘reports_to’

end

class Person < ActiveRecord::Base
belongs_to :address
end

fixtures for people

Nicolas:
first_name: Nicolas
type: Employee
reports_to: david

David:
first_name: David
type: Manager

after loading the fixtures, in the console

e=Employee.find(:first)
=> #<Employee id: 5038945, type: “Employee”, title_id: nil, first_name:
“Nicolas”, reports_to: 0, created_at: “2008-07-20 18:33:49”, updated_at:
“2008-07-20 18:33:49”>

e.reports_to
=> 0

Loading fixtures doens’t seems to associate my 2 objects, do know what’s
wrong ?

On 20 Jul 2008, at 18:20, nico Itkin wrote:

type: Manager

That should be reports_to: David
(it must exactly match the fixture label)

Fred

Frederick C. wrote:

On 20 Jul 2008, at 18:20, nico Itkin wrote:

type: Manager

That should be reports_to: David
(it must exactly match the fixture label)

Fred

problem problem with

Nicolas:
first_name: Nicolas
type: Employee
reports_to: David

Nicolas is still reporting to 0 instead of David id …

– help –

On 20 Jul 2008, at 18:43, nico Itkin wrote:

Fred

problem problem with

Nicolas:
first_name: Nicolas
type: Employee
reports_to: David

I’m being slow - you need to use the association name, not the column
name (ie manager: David)

Fred

Frederick C. wrote:

On 20 Jul 2008, at 18:43, nico Itkin wrote:

Fred

problem problem with

Nicolas:
first_name: Nicolas
type: Employee
reports_to: David

I’m being slow - you need to use the association name, not the column
name (ie manager: David)

Fred

thanks works great now !!

nico Itkin wrote:

Frederick C. wrote:

On 20 Jul 2008, at 18:43, nico Itkin wrote:

Fred

problem problem with

Nicolas:
first_name: Nicolas
type: Employee
reports_to: David

I’m being slow - you need to use the association name, not the column
name (ie manager: David)

Fred

thanks works great now !!

a last question of you’re still here : how to implement via collections
has_and_belongs_to_many ?

Frederick C. wrote:

Nicolas:
first_name: Nicolas
type: Employee
reports_to: David

I’m being slow - you need to use the association name, not the column
name (ie manager: David)

Does this mean the fixture system now reads and navigates the Models’
has_many
etc associations? That’s new - and incredibly needed!

(Our 1.2.x fixtures at work have grown so dense that adding new ones, to
set new
scenarios up, is a positive drain on our velocity!)


Phlip

On 20 Jul 2008, at 19:46, nico Itkin wrote:

first_name: Nicolas
thanks works great now !!

a last question of you’re still here : how to implement via
collections
has_and_belongs_to_many ?

This is in the docs: http://api.rubyonrails.com/classes/Fixtures.html
but in a nutshell:

  • you don’t need to specify the join table at all
  • just comma separate the labels of the appropriate records eg if an
    employee had and belongs to many projects you might have

Nicholas:
projects: death_star, relaunch, nemesis

to say that that user is a member of those projects.

Fred

Phlip

yes

On 20 Jul 2008, at 19:51, Phlip wrote:

Does this mean the fixture system now reads and navigates the
Models’ has_many
etc associations? That’s new - and incredibly needed!

It was new in 2.0. http://api.rubyonrails.com/classes/Fixtures.html
has some examples and googling for ‘foxy fixtures’ should turn up some
more.

Fred

This is in the docs: http://api.rubyonrails.com/classes/Fixtures.html
but in a nutshell:

  • you don’t need to specify the join table at all
  • just comma separate the labels of the appropriate records eg if an
    employee had and belongs to many projects you might have

Nicholas:
projects: death_star, relaunch, nemesis

to say that that user is a member of those projects.

Fred

thanks a lot Fred.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs