2010-02-23 66 views
1

我有一個事件模型,has_and_belongs_to_many藝術家IDS形式

class Event < ActiveRecord::Base 
has_and_belongs_to_many :humans, :foreign_key => 'event_id', :association_foreign_key => 'human_id' 
end 

形式爲事件插入,我把一個隱藏字段爲藝術家IDS:

<%= event_form.text_field :artist_ids %> 

如果我插入手動將值「8,9,10」(ids與2行人相關)和我提交表格,在控制器中我只獲得8個。

爲什麼?

我該怎麼辦?

+0

好像你「事件」事件模型可能是多態的。是這樣嗎? –

回答

2

當指定字符串"8,9,10"artist_ids它被轉換成一個整數值:

>> a.artist_ids = '1,2,3' 
=> "1,2,3" 
>> a.artist_ids 
=> [1] 

你需要將它傳遞到模型之前,把它分解:

>> a.artist_ids = '1,2,3'.split(',') 
=> ["1", "2", "3"] 
>> a.artist_ids 
=> [1, 2, 3] 
0

在你的「新」動作中,你如何找到artist_ids? 您需要在表單提交的操作中使用相同的查詢。 (所以如果新提交創建,您需要在創建動作中找到藝術家)。