2013-08-05 16 views
0

我在我的組織結構模型中有一個部門字段,我想將部門列表作爲數組存儲。我有這個在我的模型:序列化到數組在Rails中顯示空括號和格式不正確

class Org < ActiveRecord::Base 
    serialize :department, Array 
    attr_accessible :name, :department 
    before_validation :update_department 
    validates :name, presence: true 
    def update_department 
    if department_changed? and department.is_a?(String) 
    self.department = self.department.split(',').collect(&:strip) 
    end 
    end 
end 

和視圖:

<%= f.text_area :department, :cols => "10", :rows => "10" %> 

現在每當我嘗試註冊時,該部門字段[]現在,當我嘗試更新,該部門已經[「[department1」,「department2」]]。

我想在註冊時刪除[],並在更新時顯示department1,department2。另外數組保存不正確,應該是[「department1」,「department2」]。

請幫忙。

回答

1

你應該用逗號

object.join(',') 

在你的榜樣加入陣列:

<%= f.text_area :department,value: @org.department.join(','), :cols => "10", :rows => "10" %>