# Problemi con habtm

**URL:** <https://www.ruby-forum.com/t/problemi-con-habtm/110746>\
**Category:** IT\
**Created:** [July 30, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/problemi-con-habtm/110746 "2007-07-30T15:57:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![spliffer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@spliffer](https://www.ruby-forum.com/u/spliffer)\
**Post date:** [July 30, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/problemi-con-habtm/110746/1 "2007-07-30T15:57:59Z")

</div>

Ciao a tutti sono un newbie di RoR e ho qualche problemino con due  
tabelle DB2, collegate tra loro da una join table.

Tabella dtset:

-id (pk)  
-name  
-version

Tabella file\_list:

-id (pk)  
-name  
-filename  
-id\_file\_type

Tabella dtset\_file (join table)  
-id\_dts  
-id\_file

Nel model dtset ho:

class Dtset \< ActiveRecord::Base  
set\_table\_name ‘dtset’  
has\_and\_belongs\_to\_many :file\_lists, :join\_table =\> ‘dtset\_file’,  
:foreign\_key =\> ‘id\_dts’, :association\_foreign\_key =\> ‘id\_file’

Nel model file\_list ho:

class FileList \< ActiveRecord::Base  
set\_table\_name ‘file\_list’  
has\_and\_belongs\_to\_many :dtsets, :join\_table =\> ‘dtset\_file’,  
:foreign\_key =\> ‘id\_file’, :association\_foreign\_key =\> ‘id\_dts’

Io vorrei creare una pagina dove visualizzo tutti i file (della tabella  
file\_list) associata al dtset con id = 1. Inoltre i file devono avere  
id\_file\_type = 0, 6, 5, 4, 3

Nel dtsets\_controller ho creato un metodo:

def set\_dtset  
@dtset = Dtset.find(1)  
@dtset.file\_lists = FileList.find(:all, :conditions =\> {:id\_file\_type  
=\> [0, 6, 4, 5, 3]})  
end

Nella view ho messo:

\<%= @dtset.id %\> \<%= @dtset.name %\>

\<% for file\_lists in @dtset.file\_lists %\>

 \<%=h file\_lists.id %\> \<%=h file\_lists.name%\> \<%=h file\_lists.id\_file\_type%\> \<% end %\>

Questo funziona, il mio problema Ã¨ che nella join table, se Ã¨ vuota, mi  
inserisce tutti i file con id\_file\_type = 0, 6, 5, 4, 3 e li associa a  
id\_dts = 1.  
Questo Ã¨ il log dell’applicazione:

SELECT \* FROM dtset WHERE (dtset.id = 1)  
SELECT \* FROM file\_list WHERE (file\_list.id\_file\_type IN (0,6,4,5,3))  
SELECT \* FROM file\_list INNER JOIN dtset\_file ON file\_list.id =  
dtset\_file.id\_file WHERE (dtset\_file.id\_dts = 1 )  
INSERT INTO dtset\_file (id\_file, description, id\_dts) VALUES (64, NULL,  
1)  
INSERT INTO dtset\_file (id\_file, description, id\_dts) VALUES (3, NULL,  
1)

Per capirci non vorrei che facesse le operazioni di insert.

Spero di essere stato comprensibile, scusate la lunghezza della mail ma  
non sapevo come spiegarmi in maniera piÃ¹ breve.

Un Saluto
