Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature: attribute scoped sequence #1640

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kakubin
Copy link

@kakubin kakubin commented May 6, 2024

New feature useful in the following case

ActiveRecord::Schema.define do
  create_table :todo_lists do; end

  create_table :todo_items do |t|
    t.references :todo_list, null: false
    t.integer :priority, default: 1, null: false
    t.index [:todo_list_id, :priority], unique: true
  end
end

FactoryBot.define do
  factory :todo_item do
    scoped_sequence(:priority, :todo_list_id)
  end
end

attributes_for(:todo_item, todo_list_id: 1) # => {:todo_list_id=>1, :priority=>1}
attributes_for(:todo_item, todo_list_id: 1) # => {:todo_list_id=>1, :priority=>2} # generate attribute value `priority` sequentially
attributes_for(:todo_item, todo_list_id: 2) # => {:todo_list_id=>2, :priority=>1} # when value of scope attribute is different, another sequential number is generated.
attributes_for(:todo_item, todo_list_id: 1) # => {:todo_list_id=>1, :priority=>3}

@kakubin kakubin requested a review from mike-burns as a code owner May 6, 2024 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant