active_flag icon indicating copy to clipboard operation
active_flag copied to clipboard

Document usage with Model.insert_all

Open wscourge opened this issue 5 years ago • 1 comments
trafficstars

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.

wscourge avatar Sep 20 '20 09:09 wscourge

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?

kenn avatar Mar 24 '22 23:03 kenn

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.

kenn avatar Aug 10 '22 00:08 kenn