active_flag
active_flag copied to clipboard
Document usage with Model.insert_all
It would be very helpful if there was a documented usage of ActiveFlag with insert_all new to the Ruby on Rails 6.
Consider the following dummy example of db/seeds.rb, where the langauges column uses ActiveFlag.
if Rails.env.development?
now = Time.now
users = []
100.times do |n|
users << {
name: FFaker::Name.name,
email: FFaker::Internet.unique.email,
languages: User.languages.to_a.shuffle[0..2],
created_at: now,
updated_at: now
}
end
User.insert_all(users)
end
I have also tried User.languages.maps.values.shuffle[0..2] and some other variations, all resulting in the following error:
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "languages" violates not-null constraint
Is there a simple way to do so? If you show me how, I don't mind opening PR with README.md updates.
Cheers and thank you for the great work you did implementing this gem.
I'm not familiar with insert_all as I've been using https://github.com/zdennis/activerecord-import, but what happens if you directly pass the integer value with user.languages.raw?
Confirmed you can do this:
$ bin/console
> profile = Profile.new
=> #<Profile id: nil, languages: 0, figures: nil>
> profile.languages = Profile.languages.keys.shuffle.take(2)
=> [:spanish, :english]
> profile.languages.raw
=> 24
If that's what you wanted.