Problema fixtures

Salve ragazzi,
ho riscontrato la seguente stranezza con le fixtures.

Ho due modelli:

class Entity < ActiveRecord::Base
has_many :users
belongs_to :owner, :class_name => ‘User’

validates_presence_of :owner

end

class User < ActiveRecord::Base
has_many :entities # entities created by the user (he is its owner)
belongs_to :entity # entity (one) the user belongs to
end

Giocando in console tutto va a meraviglia, setto l’owner, la
validazine lavora…tutto ok.

Avendo questa fixture:

– entities.yml

prima_entity:
name: Una entità a caso
description: nulla da dire
url: http://www.example.org
owner: carlo

il problema nasce con la riga “owner”, il DB non vuol saperne, la
mappa nella colonna “owner” (che ovviamene non esiste nel DB, è
presente solo user_id).
Ho provato anche con:
user: carlo
ma niente…

Any tips?

Ciao Carlo,
Questa è una relazione ‘molti a molti’, dovresti gestirla con
has_and_belongs_to_many:

http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many

class Entity < ActiveRecord::Base
has_and_belongs_to_many :owners, :class_name => ‘User’
end

class User < ActiveRecord::Base
has_and_belongs_to_many :entities
end

Inoltre dovresti creare una join table chiamata esattamente
‘entities_users’.

C’è un altro modo per gestire le relazioni habtm, fortemente
raccomandata nel caso in cui tu voglia aggiungere attributi alla join
table, che così diventa un model object a tutti gli effetti.

class Ownership < ActiveRecord::Base
belongs_to :user
belongs_to :entity
end

class Entity < ActiveRecord::Base
has_many :ownerships
has_many :owners, :through => :ownerships, :source => :user
end

class User < ActiveRecord::Base
has_many :ownerships
has_many :entities, :through => :ownerships
end

Luca.

Un attimo un attimo… mi sono spiegato male.
Rettifico:

  • un utente può creare molte entità, ogni entità ha un unico
    creatore (chiamato owner)
  • un utente può essere associato ad una sola entità (non
    necessariamente creata da lui…)

Per questo pensavo al modello proposto… Quello che mi lascia
perplesso è il malfunzionamento delle fixtures…

2008/1/26, Luca G. [email protected]:

Ho risolto il “problema” (che comunque avevo mal posto… sorry).

Ogni entità è creata da un utente, il quale è l’ “owner” per tale
entità.Inoltre un utente può, facoltativamente, essere associato ad una
particolare entità (non necessariamente creata da lui stesso).

Quanto appena detto nel codice diventa:

db/migrate/002_create_entity

class CreateEntities < ActiveRecord::Migration
def self.up
create_table :entities do |t|
t.column :name, :string
t.column :owner_id, :integer
t.timestamps
end
add_column :users, :entity_id, :integer
end

def self.down
remove_column :users, :entity_id
drop_table :entities
end
end

app/models/user.rb

class User < ActiveRecord::Base
has_many :entities # entities created by the user (that’s its owner)
belongs_to :entity # entity the user belongs to
end

app/models/entity.rb

class Entity < ActiveRecord::Base
has_many :users
belongs_to :owner, :class_name => ‘User’, :foreign_key => ‘owner_id’

validates_presence_of :owner
end

db/fixtures/entities.yml

pippo:
name: Pippo
owner: carlo

db/fixtures/users.yml

carlo:
username: carlo
entity: pippo

Adesso il seguente test non mi dà alcun problema:

test/unit/entity_test.rb

require File.dirname(FILE) + ‘/…/test_helper’

class EntityTest < ActiveSupport::TestCase
def setup
@e = entities(:pippo)
end

def test_valid_owner
@e.owner = nil
assert !@e.valid?
end
end

In realtà “era ovvio” (a posteriori…), tutto sta nel definire la
corretta FK nella tabella entities ed associarla opportunamente nel
modello:

db/migrate/002_create_entity

t.column :owner_id, :integer

app/models/entity.rb

belongs_to :owner, :class_name => ‘User’, :foreign_key => ‘owner_id’
validates_presence_of :owner

In questo modo possiamo avere più “ruoli” per l’associazion
utente-entità (es: creatore, manuntentore, ecc.)