Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 由于字段名不同,Gem会创建MySQL错误消息_Ruby On Rails_Ruby_Ruby On Rails 3_Gem - Fatal编程技术网

Ruby on rails 由于字段名不同,Gem会创建MySQL错误消息

Ruby on rails 由于字段名不同,Gem会创建MySQL错误消息,ruby-on-rails,ruby,ruby-on-rails-3,gem,Ruby On Rails,Ruby,Ruby On Rails 3,Gem,我正在扮演可标记轨道宝石的角色。我收到了以下消息,因为我相信标签模型正在寻找“post_id”,而Posts模型/表中不存在“id” 我是否需要修改模型或控制器以使其正常工作 当我试图显示帖子时出错 更新: -该错误似乎是由post view show.html.erb引起的。post视图引用post模型标记列表 错误: Mysql2::Error: Unknown column 'taggings.post_id' in 'where clause': SELECT `tags`.* FRO

我正在扮演可标记轨道宝石的角色。我收到了以下消息,因为我相信标签模型正在寻找“post_id”,而Posts模型/表中不存在“id”

我是否需要修改模型或控制器以使其正常工作

当我试图显示帖子时出错

更新: -该错误似乎是由post view show.html.erb引起的。post视图引用post模型标记列表

错误:

 Mysql2::Error: Unknown column 'taggings.post_id' in 'where clause': SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`post_id` = 4
后控制器显示

def show
  @post = Post.find(params[:id])

 respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @post }
   end
 end
Post show.html.erb:

<%= form_for(@post) do |post_form| %>
..
<div class="field">
  <%= post_form.label :tag_list, "Tags (separated by commas)" %><br />
  <%= post_form.text_field :tag_list %>
</div>
后模型:

class Post < ActiveRecord::Base
  attr_accessible :content, :name, :title, :tag_list #:tags_attributes

  has_many :taggings
  has_many :tags, through: :taggings

  def self.tagged_with(name)
Tag.find_by_name!(name).posts
  end

  def self.tag_counts
    Tag.select("tags.*, count(taggings.tag_id) as count").
        joins(:taggings).group("taggings.tag_id")
  end

  def tag_list
    tags.map(&:name).join(", ")
  end

 def tag_list=(names)
    self.tags = names.split(",").map do |n|
      Tag.where(name: n.strip).first_or_create!
    end
  end

#Validations
  validates :name,  :presence => true
  validates :title, :presence => true,
        :length => { :minimum => 5 }

  has_many :comments, :dependent => :destroy

#  accepts_nested_attributes_for :tags, :allow_destroy => :true,
#                                :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
标签型号:

class Tag < ActiveRecord::Base
 belongs_to :post
 has_many :taggings
 has_many :posts, through: :taggings
end
标记模型:

class Tagging < ActiveRecord::Base
 belongs_to :tag
belongs_to :post
end

经过几个小时的胡闹,我终于成功了。基本上,我必须删除所有以前对标记的引用——特别是在post controller和post视图中

然后,我按照下面的链接所描述的步骤安装Acts As Taggable gem。


我不得不从我的Posts模型中删除名为“name”的字段,因为这会导致标记模型中的字段名“name”发生冲突。

您也可以显示迁移文件吗?谢谢Ross-我自己以前的迁移破坏了标记gem的功能。我必须卸载并重新开始。
# add acts as taggable on gem, for tagging (clearly optional)
# edit file: Gemfile
+gem 'acts-as-taggable-on'

# execute bundle to install gems
$ bundle

# create Post model
$ rails generate model Post title:string content:text

# add taggable property to Post model & make mass-assignable
# file: app/models/post.rb
 class Post < ActiveRecord::Base
+  attr_accessible :content, :title, :tag_list
+  acts_as_taggable_on :tags
 end

# run acts as taggable migration
$ rails generate acts_as_taggable_on:migration

# setup database
$ rake db:migrate