小编wei*_*ney的帖子

rails model querying returns 11 records but no limit is set

I am querying a set of data using a custom model Stat based on ActiveRecord, using sqlite3. It is obvious that the total amount of data is more than 100, but when I do my querying of all data, it always returns 11 records, no more of other data, and I did not set any limit to the statement, but in console it just add a limit of 11. limit Below is my code:

2.5.1 001 > Stat.all …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails limit

5
推荐指数
1
解决办法
2691
查看次数

查询使用FactoryBot创建has_many&belongs_to记录

我正在编写Rspec requests spec,在此之前我想使用构建一些测试数据FactoryBot.

现在我有一个模型Game:

class Game < ApplicationRecord
  has_many :game_levels
Run Code Online (Sandbox Code Playgroud)

和模型GameLevel:

class GameLevel < ApplicationRecord
  belongs_to :game
Run Code Online (Sandbox Code Playgroud)

在我的/spec/factories/game.rb:

FactoryBot.define do
  factory :game do
    name { :Mario }
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的spec/factories/game_level.rb:

FactoryBot.define do
  factory :game_level do
    name { :default }
    min_level { 0 }
    max_level { 100 }

    game
  end
end
Run Code Online (Sandbox Code Playgroud)

在我spec/requests/user_plays_game_spec.rb,我只是编写代码来创建游戏和game_level,并打印game.id,game_level.game_id.我发现它们不一样.此外,game.game_levels回报nil.

before(:all) do
  @game = create(:game)
  @game_level = create(:game_level) …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many belongs-to factory-bot

0
推荐指数
1
解决办法
53
查看次数