2013-07-29 81 views
0

是否有Ruby gem幫助我們構建允許用戶爲應用程序實體/表單定義自定義屬性的應用程序?這在ERP或CRM應用程序中非常常見。自定義屬性/使用Ruby on Rails的用戶定義屬性

我看着https://github.com/latortuga/has_magic_columns,它看起來很有希望,但自述文件說它不是生產就緒。我也不喜歡這樣的事實,即代碼在同一個字符串列中存儲不同的數據類型。

我可以使用NoSQL數據庫來幫助我將自定義屬性存儲在無模式數據庫中,但我仍然需要創建一個存儲自定義屬性配置的數據庫結構。

回答

1

您可以使用ActiveRecord ::商店這對Rails 3.2出來了。它給你,你尋找什麼:

  • 商店,序列化到一個文本列在數據庫
  • 你把任何屬性裏面,你想
  • 另外,你可以定義訪問器共同屬性

實施例:

class User < ActiveRecord::Base 
    store :settings, accessors: [ :color, :homepage ], coder: JSON 
end 

u = User.new(color: 'black', homepage: '37signals.com') 
u.color       # Accessor stored attribute 
u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor 

# There is no difference between strings and symbols for accessing custom attributes 
u.settings[:country] # => 'Denmark' 
u.settings['country'] # => 'Denmark' 

# Add additional accessors to an existing store through store_accessor 
class SuperUser < User 
    store_accessor :settings, :privileges, :servants 
end 

從/更多信息粘貼: http://api.rubyonrails.org/classes/ActiveRecord/Store.html