我正在做这个教程,并且陷入了标记部分.基本上我有文章可以有一个标签列表.由于一篇文章可以有多个标签,反之亦然,因此还有一个额外的taggings
模型,通过该模型建立这种关联.以下是模型:
class Article < ActiveRecord::Base has_many :comments has_many :taggings has_many :tags, through: :taggings end class Tag < ActiveRecord::Base has_many :taggings has_many :articles, through: :taggings end class Tagging < ActiveRecord::Base belongs_to :tag belongs_to :article end
和迁移:
def change create_table :articles do |t| t.string :title t.text :body t.timestamps end create_table :tags do |t| t.string :name t.timestamps end create_table :taggings do |t| t.references :tag, index: true t.references :article, index: true t.timestamps end
还有article_controller
(其中包括):
def create @article = Article.new(article_params) @article.save redirect_to article_path(@article) end
现在,正如教程所说,当我尝试tag
使用rails控制台为文章创建一个新的时,我得到NoMethodError
一个nil:NilClass
:
head :011 > Article.first.tags.create(name: "tag") Article Load (0.5ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT 1 (0.2ms) begin transaction SQL (0.8ms) INSERT INTO "tags" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", ...], ["name", "tag"], ["updated_at", ...]] SQL (2.1ms) INSERT INTO "taggings" ("article_id", "created_at", "tag_id", "updated_at") VALUES (?, ?, ?, ?) [["article_id", 1], ["created_at", ...], ["tag_id", 7], ["updated_at", ...]] (0.6ms) rollback transaction NoMethodError: undefined method `name' for nil:NilClass
所以在我看来,好像tag
创建了条目,以及正确的taggings
条目,但显然在某些时候它很难找到正确的tag
,因此错误.我对吗?我怎样才能解决这个问题?
关于这种错误,我在SO上发现了很多问题,但每个问题都是由于我无法与之相关的一些问题引起的......
更新:
reloading
或重新启动rails控制台无效.
这是错误回溯,路径为~/.rvm/gems/ruby-head/gems/activerecord-4.0.4/lib/active_record
:
from path/associations/has_many_association.rb:81:in `cached_counter_attribute_name' from path/associations/has_many_association.rb:77:in `has_cached_counter?' from path/associations/has_many_association.rb:85:in `update_counter' from path/associations/has_many_through_association.rb:66:in `insert_record' from path/associations/collection_association.rb:463:in `block (2 levels) in create_record' from path/associations/collection_association.rb:367:in `add_to_target' from path/associations/collection_association.rb:461:in `block in create_record' from path/associations/collection_association.rb:152:in `block in transaction' from path/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' from path/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction' from path/connection_adapters/abstract/database_statements.rb:213:in `transaction' from path/transactions.rb:209:in `transaction' from path/associations/collection_association.rb:151:in `transaction' from path/associations/collection_association.rb:460:in `create_record' from path/associations/collection_association.rb:121:in `create' from path/associations/collection_proxy.rb:260:in `create' from (irb):14 from ~/.rvm/gems/ruby-head/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start' from ~/.rvm/gems/ruby-head/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start' from /.rvm/gems/ruby-head/gems/railties-4.0.4/lib/rails/commands.rb:62:in `' from bin/rails:4:in `require' from bin/rails:4:in ` '
tschale.. 15
我通过切换ruby版本来实现它.我正在使用ruby-head
,这是ruby 2.2.0dev
.切换回来ruby-2.1.1
,现在它可以正常工作.应该早点尝试过......
也许这可以帮助其他人面临类似的错误.