Ruby on rails Rails ActiveRecord更新字段

Ruby on rails Rails ActiveRecord更新字段,ruby-on-rails,activerecord,model,Ruby On Rails,Activerecord,Model,我的例子是: class Category < ActiveRecord::Base has_many :tags, :as => :tagable, :dependent => :destroy def tag_string str = '' tags.each_with_index do |t, i| str+= i > 0 ? ', ' : '' str+= t.tag end str end

我的例子是:

class Category < ActiveRecord::Base
  has_many :tags, :as => :tagable, :dependent => :destroy

  def tag_string
    str = ''
    tags.each_with_index do |t, i|
      str+= i > 0 ? ', ' : ''
      str+= t.tag
    end
    str
  end

  def tag_string=(str)
    tags.delete_all
    Tag.parse_string(str).each { |t| tags.build(:tag => t.strip) }
  end

end
类别:tagable,:dependent=>:destroy
def标记字符串
str=''
tags.each_与_索引do|t,i|
str+=i>0?',':''
str+=t.tag
结束
str
结束
def tag_字符串=(str)
tags.delete_all
parse_string(str).each{t|tags.build(:Tag=>t.strip)}
结束
结束
您将如何优化此标记字符串字段?我不想每次更新标签时都删除所有标签。有没有更好的方法来解析带有标记的字符串?
我不想使用插件!Thx.

我知道你不想使用插件,但你可能想深入研究插件的源代码,看看他们是如何处理这些情况的。根据我的经验,使用该插件非常轻松。

我知道你不想使用插件,但你可能想深入了解插件的来源,看看他们是如何处理这些情况的。根据我的经验,使用该插件非常轻松。

class-Categoryclass Category < ActiveRecord::Base
  has_many :tags, :as => :tagable, :dependent => :destroy

  def tag_string
    tags.map {|t| t.name }.join ', '
  end

  def tag_string=(str)
    tags = Tag.parse_string(str)
  end

end
有多个:标记,:as=>:tagable,:dependent=>:destroy def标记字符串 tags.map{| t | t.name}.join',' 结束 def tag_字符串=(str) tags=Tag.parse_字符串(str) 结束 结束 我不知道Tag.parse_string(str)方法做什么。如果它返回一个
Tag
对象数组,那么我的示例应该可以工作。我不确定这是否只会更新,或者删除旧的并添加新的。您可以测试它,并在日志中查看它的实际功能。

class-Category:tagable,:dependent=>:destroy
def标记字符串
tags.map{| t | t.name}.join','
结束
def tag_字符串=(str)
tags=Tag.parse_字符串(str)
结束
结束

我不知道Tag.parse_string(str)方法做什么。如果它返回一个
Tag
对象数组,那么我的示例应该可以工作。我不确定这是否只会更新,或者删除旧的并添加新的。您可以测试它,并在日志中查看它的实际功能。

我同意其他评论者的看法。你最好在这里使用插件。这里有一个解决方案

 class Category < ActiveRecord::Base
  has_many :tags, :as => :tagable, :dependent => :destroy

  def tag_string
    tags.collect(&:name).join(", ")
  end

  def tag_string=(str)
    # Next line will delete the old association and create
    # new(based on the passed str). 
    # If the Category is new, then save it after the call.
    tags = Tag.create(str.split(",").collect{ |name| {:name => name.strip} })
  end

end
类别:tagable,:dependent=>:destroy
def标记字符串
tags.collect(&:name).join(“,”)
结束
def tag_字符串=(str)
#下一行将删除旧关联并创建
#新建(基于传递的str)。
#如果类别是新的,请在通话后保存。
tags=Tag.create(str.split(“,”).collect{{name}{:name=>name.strip})
结束
结束

我同意其他评论。你最好在这里使用插件。这里有一个解决方案

 class Category < ActiveRecord::Base
  has_many :tags, :as => :tagable, :dependent => :destroy

  def tag_string
    tags.collect(&:name).join(", ")
  end

  def tag_string=(str)
    # Next line will delete the old association and create
    # new(based on the passed str). 
    # If the Category is new, then save it after the call.
    tags = Tag.create(str.split(",").collect{ |name| {:name => name.strip} })
  end

end
类别:tagable,:dependent=>:destroy
def标记字符串
tags.collect(&:name).join(“,”)
结束
def tag_字符串=(str)
#下一行将删除旧关联并创建
#新建(基于传递的str)。
#如果类别是新的,请在通话后保存。
tags=Tag.create(str.split(“,”).collect{{name}{:name=>name.strip})
结束
结束

为什么不想使用插件?其他人已经对这个功能进行了完美的编码和测试,所以您不必这么做。问题中描述的标记关系效率低下、笨拙,并且无法使用标记的最佳功能标记。为什么不使用插件?其他人已经对这个功能进行了完美的编码和测试,所以您不必这么做。问题中描述的标记关系效率低下、笨拙,并且使标记的最佳功能标记无法使用。