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
Ruby on rails Rails:为什么我的设备具有相同的ID?_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails:为什么我的设备具有相同的ID?

Ruby on rails Rails:为什么我的设备具有相同的ID?,ruby-on-rails,Ruby On Rails,我已经开始写一个测试: class PostPresenterTest < ActionView::TestCase let(:presenter) { PostPresenter.new(post, view) } let(:post) { Post.first } it 'should something something...' do byebug end end 当我进入byebug时,我注意到Post.count==2,但是各个Post具有相同的id

我已经开始写一个测试:

class PostPresenterTest < ActionView::TestCase
  let(:presenter) { PostPresenter.new(post, view) }
  let(:post) { Post.first }

  it 'should something something...' do
    byebug
  end
end
当我进入byebug时,我注意到Post.count==2,但是各个Post具有相同的id:

  Post.first.id == 298486374
  Post.last.id == 298486374

这是预期的吗?我需要确保每个帖子都有自己独特的ID。rails为固定装置添加ID的标准是什么?我应该手动添加ID,还是应该采取特定的步骤来确保帖子具有不同的ID?

问题是我假设Post.first和Post.last返回不同的帖子。他们实际上返回了相同的邮件。ID是不同的

(byebug) Post.all
#<ActiveRecord::Relation [#<Post id: 298486374, title: "Title Two", content: "Second content.", created_at: "2017-06-01 06:44:50", updated_at: "2017-06-01 06:44:50">, #<Post id: 980190962, title: "Title One", content: "First content.", created_at: "2017-06-01 06:44:50", updated_at: "2017-06-01 06:44:50">]>
我尝试了Post.ordercreated_at.first和Post.ordercreated_at.first,但它们也返回了相同的帖子。我意识到这是因为两篇文章都有相同的创建日期。手动添加日期修复了此问题。“最后一个”和“第一个”现在返回不同的帖子。

因此,省略ID将导致自动生成ID。
(byebug) Post.all
#<ActiveRecord::Relation [#<Post id: 298486374, title: "Title Two", content: "Second content.", created_at: "2017-06-01 06:44:50", updated_at: "2017-06-01 06:44:50">, #<Post id: 980190962, title: "Title One", content: "First content.", created_at: "2017-06-01 06:44:50", updated_at: "2017-06-01 06:44:50">]>