使用 has_many 通过关联创建得到 NoMethodError(nil:NilClass 的未定义方法“名称") [英] create with has_many through association gets NoMethodError (undefined method `name' for nil:NilClass)
问题描述
我正在做本教程,但被困在标记部分.基本上我有可以有标签列表的文章.由于一篇文章可以有多个标签,反之亦然,因此有一个额外的 taggings
模型,通过该模型对这种关联进行建模.以下是模型:
I'm doing this tutorial and am stuck in the Tagging part. Basically I have articles that can have a list of tags. As one article can has multiple tags and vice versa, there is an additional taggings
model, through which this association is modelled. Here are the models:
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
与(除其他外):
There's also an article_controller
with (amongst others):
def create
@article = Article.new(article_params)
@article.save
redirect_to article_path(@article)
end
现在,正如教程所建议的那样,当我尝试使用 rails 控制台为文章创建一个新的 tag
时,我收到 NoMethodError
的 nil:NilClass
:
Now, as the tutorial suggets, when I try to create a new tag
with the rails console for an article, I get a NoMethodError
for a 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 it seems to me, as if the tag
entry is created, as well as the correct taggings
entry, but apparently at some point it struggles to find the correct tag
, hence the error. Am I right? How can I fix this?
我在 SO 上发现了很多关于此类错误的问题,但每个问题都是由一些与我的问题无关的问题引起的...
I found a lot of questions on SO regarding this kind of error, but each was caused by some problem I couldn't relate to mine...
更新:
reloading
或者重启 rails 控制台都没有效果.
reloading
or restarting the rails console has no effect.
这是错误回溯,路径为~/.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 `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
推荐答案
我通过切换 ruby 版本让它工作.我使用的是 ruby-head
,也就是 ruby 2.2.0dev
.切换回 ruby-2.1.1
,现在可以正常工作了.应该早点尝试...
I got it to work by switching ruby versions. I was using ruby-head
, which is ruby 2.2.0dev
. Switched back to ruby-2.1.1
, now it works without errors. Should've probably tried that earlier...
也许这可以帮助其他面临类似错误的人.
Maybe this could help others facing similar errors.
这篇关于使用 has_many 通过关联创建得到 NoMethodError(nil:NilClass 的未定义方法“名称")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!