能够按照以下方式管理habtm,我想要一个更好的方法来实现这个目标
我在Rails 3上的用户和标签之间有一个habtm,aa 0.5.1 标签名称是uniq
f.input :tags, :label => 'Assign existing tag'
# this above allows to select from existing tags, but cannot allow to create one
f.has_many :tags, :label => 'Add new tags, modify existings' do |ff|
ff.input :name
ff.input :_destroy, :as => :boolean
end
# this above allows to create new one but not allow to specify existing one
# if we specify existing one, uniqueness wont let create this one, neither existing get used
# and throws validation error
任何提示?
添加我的模型
class User < ActiveRecord::Base
has_and_belongs_to_many :tags
scope :tagged_with, lambda {|tags| joins(:tags).where("tags.name" => tags)}
accepts_nested_attributes_for :tags, :allow_destroy => true
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :users
validates :name, :uniqueness => { :case_sensitive => false }
end
答案 0 :(得分:0)
试试这个,
f.label => 'Assign existing tag'
f.select :tags, Tag.all.map{|t| [t.tag_name, t.id]}, {:prompt => "Select Tag name" }
第二件事是在模型中添加这一行,
validates :tags, :uniqueness => {:scope => :tag_name}
根据你的问题,这只是一个想法。这不是您的确切答案,因为您的规范不足以为您提供确切的答案。