# \[Help\] Come aggiungere un legame nella join table, "soft"

**URL:** <https://www.ruby-forum.com/t/help-come-aggiungere-un-legame-nella-join-table-soft/159741>\
**Category:** IT\
**Created:** [February 8, 2009, 12:24am UTC](https://www.ruby-forum.com/t/help-come-aggiungere-un-legame-nella-join-table-soft/159741 "2009-02-08T00:24:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![triplem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@triplem](https://www.ruby-forum.com/u/triplem)\
**Post date:** [February 8, 2009, 12:24am UTC](https://www.ruby-forum.com/t/help-come-aggiungere-un-legame-nella-join-table-soft/159741/1 "2009-02-08T00:24:13Z")

</div>

Quesito 1.  
Ho due modelli:

class Engine \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :technologies, :join\_table =\>  
“enginetechnologies”  
end

class Technology \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :engines, :join\_table =\> “enginetechnologies”  
end

Quando creo un nuovo engine voglio associare una o piÃ¹ technologies.

Engine.find(1).technologies\<\<Technology.find(1)\<\<Technology.find(2)

ok, ma al successivo:

Engine.find(2).technologies\<\<Technology.find(1)\<\<Technology.find(2)

PRIMARY KEY must be unique: INSERT INTO “enginetechnologies”  
(“updated\_at”, “engine\_id”, “technology\_id”, “id”, “created\_at”) VALUES  
(‘2009-02-03 18:25:49’, 2, 1, 1, ‘2009-02-03 18:25:49’)

perchÃ¨ l’id della join Ã¨ sempre 1??? (il db Ã¨ sqlite3 e la tabella ha  
l’id autoincrement)

Quesito 2.  
Se volessi ottimizzare le performance ed evitare la select ogni volta  
che aggiungo una technology (al di lÃ del discorso cache), come posso  
fare?

Grazie ciao.

---

<div class="post-metadata">

**Author:** ![triplem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b19c9b/32.png) [@triplem](https://www.ruby-forum.com/u/triplem)\
**Post date:** [February 8, 2009, 1:31am UTC](https://www.ruby-forum.com/t/help-come-aggiungere-un-legame-nella-join-table-soft/159741/2 "2009-02-08T01:31:03Z")

</div>

Per il quesito 1, ho eliminato la primary key ed ora funziona.  
def self.up  
create\_table :engines\_technologies, :id =\> false do |t|  
t.belongs\_to :engine  
t.belongs\_to :technology

```
  t.timestamps
end
add_index :engines_technologies, :engine_id
add_index :engines_technologies, :technology_id

```

end

…ed ho creato gli indici, non so se ha senso con un db come sqlite

il dubbio permane sul quesito 2
