试图从Rails控制台获取信息的困惑
我有以下型号:Article
& Comment
。
对于Article
,它是has_many :comments
对于Comment
,它是belongs_to :article
让我们假设他们是关联的,并且是功能性的。他们顺便说一句。
这是架构:
ActiveRecord::Schema.define(version: 20160312052519) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["article_id"], name: "index_comments_on_article_id"
end
这些是路线:
resources :articles do
resources :comments
end
当我运行如下命令时:
Article.first.comments
Comment.first.article
Comment.first.article.title
Comment.first.article.text
Comment.first.commenter
Comment.first.body
他们都工作。
但是当我尝试在Articles
上反向运行时:
Article.first.comments.commenter
Article.first.comments.body
Article.all.comments
他们不工作。为什么不?
编辑
这是我运行Article.first.comments.commenter
Article.first.comments.commenter
Article Load (23.0ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT 1
NoMethodError: Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = ? [["article_id", 1]]
undefined method `commenter' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x007fa32b7e3d00>
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:136:in `method_missing'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:99:in `method_missing'
from (irb):127
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/aa/dropbox/beginningRails/guideblogagain/bin/rails:9:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/commands/rails.rb:6:in `call'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/command_wrapper.rb:38:in `call'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:185:in `block in serve'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:156:in `fork'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:156:in `serve'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:131:in `block in run'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:125:in `loop'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:125:in `run'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
答案 0 :(得分:1)
Article.first.comments
是一个ActiveRecord_Associations_CollectionProxy
对象,类似于数组。如果您想获得每条评论的评论者,可以使用map
Article.first.comments.map(&:commenter)
或者您可以获得第一条评论的评论者:
Article.first.comments.first.commenter
同样的事情:
Article.first.comments.body # Won't work
Article.first.comments.map(&:body) # Returns an array (each comment's body)
Article.all.comments # Won't work
Article.all.map(&:comments) # Returns an array (each article's comments)
答案 1 :(得分:1)
运行工作示例Article.first.comments
时,请查看实际返回值。你看到了什么?返回是一个对象数组,对吧?所以说Article.first
有10 Comments
。运行查询时,ActiveRecord将返回10个Comment
个对象的数组。
现在让我们来看看你的一个非工作示例。
说,Article.first.comments.commenter
。我们再次从Article.first
开始,我们知道我们在DB中返回第一个Article
对象。
但下一步是考虑Article.first.comments
。
我们已经说服自己,这是一组Comment
个对象,对吗?这就是问题所在 - commenter
是单个Comment
对象的属性。
但是你没有一个Comment
对象 - 你有一个Comment
个对象数组,并且没有在数组上定义commenter
。
Article.first.comments.body
问题是一样的。这有意义吗?
您可以在控制台中执行以下操作:
Article.first.comments.each do |comment|
puts comment.commenter
end
然后您可以获得所有评论者/身体价值观。
使用这种思路,我认为你能够用Article.all.comments
说服自己解决这个问题,但如果没有,请随意评论,我们可以说出来。
Here are the docs用于Active Record关系,供参考。干杯!