Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 属于关联查询方法';不存在_Ruby On Rails_Associations - Fatal编程技术网

Ruby on rails 属于关联查询方法';不存在

Ruby on rails 属于关联查询方法';不存在,ruby-on-rails,associations,Ruby On Rails,Associations,当我创建“属于”关联时应该存在的“关联?”查询方法实际上没有被创建: class Author < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author end 类作者p=Post.create(:author=>author.create) >>作者? NoMethodError:for#的未定义方法'author' 这是一个错误,是文档错了,还是

当我创建“属于”关联时应该存在的“关联?”查询方法实际上没有被创建:

class Author < ActiveRecord::Base
  has_many :posts
end
class Post < ActiveRecord::Base
  belongs_to :author
end
类作者
>>p=Post.create(:author=>author.create) >>作者? NoMethodError:for#的未定义方法'author'
这是一个错误,是文档错了,还是我做错了什么?

去掉问号

p.author

它只对布尔属性有效,不属于关联。

当然有效,但问题是为什么“作者”不起作用。有文件证明应该这样做。嗨,你想达到什么目的?查看是否有作者与此帖子有关联?是的,但具体使用文档中描述的“post#author?”方法。我意识到我可以通过其他方式(例如布尔上下文中的p.author)获得这些信息,但我正试图找出为什么文档化的功能对我不起作用。我也无法让它起作用,似乎是一个bug。。。
p.author