Rails 5 - 使用预定义标签

时间:2016-12-18 04:50:57

标签: ruby-on-rails ruby tags acts-as-taggable-on

我正在尝试使用我的rails 5 app将这些行为用作gem上的标记。

我有一个名为Proposal的模型,我试图用我的Randd :: Fields模型中的预定义标签进行标记。

在我的提案中.rb,我有:

class Proposal < ApplicationRecord

  acts_as_taggable
  acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities

在我的提案控制器中,我将randd_fields_list属性列入白名单:

params.require(:proposal).permit(:title,:randd_fields_list)

在我的Randd :: Fields表中,我保存了一条记录:

Randd::Field.all
  Randd::Field Load (0.5ms)  SELECT "randd_fields".* FROM "randd_fields"
 => #<ActiveRecord::Relation [#<Randd::Field id: 1, created_at: "2016-11-26 08:38:11", updated_at: "2016-11-26 08:38:11", anz_reference: "test ref", title: "test title">]> 

在控制台中,我正在尝试将预定义的Randd :: Field.title添加到提案中:

Proposal.first.randd_field_list.add(Randd :: Field.find_by(id:1).title)

但是 - 当我尝试时:Proposal.first.randd_field_list,我得到:

p.randd_field_list
  ActsAsTaggableOn::Tagging Load (0.7ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2  [["taggable_id", 17], ["taggable_type", "Proposal"]]
  ActsAsTaggableOn::Tag Load (0.9ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL)  [["taggable_id", 17], ["taggable_type", "Proposal"]]
 => [] 

所以 - 这没效果。

如何使用Randd :: Field表中定义的标签更新randd_field_list?

1 个答案:

答案 0 :(得分:0)

要保存标记,您需要保存Proposal对象:

proposal = Proposal.first
proposal.randd_field_list.add(Randd::Field.find_by(id: 1).title)
proposal.save