0

I have a seeds.rb file containing

User.find_or_create_by([
  { name: 'Root', email: '[email protected]', groups: {role: 'root', countries: :all}, password: 'asdfasdf' },
  { name: 'Admin', email: '[email protected]', groups: {role: 'admin', countries: :all}, password: 'asdfasdf' },
  { name: 'Dev', email: '[email protected]', groups: {role: 'dev', countries: :all}, password: 'asdfasdf' },
  { name: 'BO', email: '[email protected]', groups: {role: 'root', countries: :all}, password: 'asdfasdf' },
  { name: 'Affiliate', email: '[email protected]', groups: {role: 'affiliate', countries: 'uk', gateways: ['Oxygen8']}, password: 'asdfasdf' }
])

But when I run rake db:seed, I get this error:

/app # rake db:seed
rake aborted!
TypeError: no implicit conversion of Hash into String
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/sanitization.rb:125:in `match?'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/sanitization.rb:125:in `sanitize_sql_array'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/sanitization.rb:26:in `sanitize_sql_for_conditions'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/relation/where_clause_factory.rb:14:in `build'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/relation/query_methods.rb:591:in `where!'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/relation/query_methods.rb:584:in `where'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/relation/finder_methods.rb:81:in `find_by'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/relation.rb:164:in `find_or_create_by'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/querying.rb:8:in `find_or_create_by'
/app/db/seeds.rb:9:in `<main>'
/usr/local/bundle/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/usr/local/bundle/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/usr/local/bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:285:in `block in load'
/usr/local/bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/usr/local/bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:285:in `load'
/usr/local/bundle/gems/railties-5.2.2/lib/rails/engine.rb:551:in `load_seed'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/tasks/database_tasks.rb:281:in `load_seed'
/usr/local/bundle/gems/activerecord-5.2.2/lib/active_record/railties/databases.rake:194:in `block (2 levels) in <main>'
/usr/local/bundle/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

I tried using User.where([{*same_value*}]).first_or_create, but still getting the same error.

This is the user.rb model

class User < ApplicationRecord

  serialize :groups
  validates :groups, presence: true

  devise :database_authenticatable, :rememberable, :trackable, :validatable,
    :omniauthable, omniauth_providers: [:google_oauth2]

  scope :admins, -> { where('groups LIKE ?', "%#{User::ADMIN}%") }

  ADMIN = 'admin'
  ROOT = 'root'
  DEV = 'dev'
  BO = 'bo'
  AFFILIATE = 'affiliate'

  def manageable_groups
    if root?
      [ROOT, ADMIN, BO, DEV, AFFILIATE]
    elsif admin?
      [ADMIN, BO, DEV, AFFILIATE]
    else
      []
    end
  end

  def formatted_slack_user_or_first_name
    formatted_slack_user || first_name
  end

  def formatted_slack_user
    "<@#{self.slack_user}>" unless self.slack_user.nil?
  end

  def first_name
    self.name.split(' ').first
  end

  def admin?
    self.groups[:role] == User::ADMIN
  end

  def affiliate?
    self.groups[:role] == User::AFFILIATE
  end

  def root?
    self.groups[:role] == User::ROOT
  end

  def dev?
    self.groups[:role] == User::DEV
  end

  def bo?
    self.groups[:role] == User::BO
  end

  def self.from_omniauth(access_token)
      data = access_token.info
      user = User.where(email: data['email'].downcase).first

      unless user
          user = User.create(name: data['name'],
            email: data['email'].downcase,
            groups: {role: User::BO, countries: :all},
            password: Devise.friendly_token[0,20]
          )
      end
      user
  end

  def permitted_gateways
    groups[:gateways]
  end

  def permitted_countries
    groups[:countries]
  end
end

2
  • Can you post the User model? Commented Jul 18, 2019 at 2:40
  • @Noah, I added the User model. Commented Jul 18, 2019 at 2:45

1 Answer 1

4

User.find_or_create_by expects attributes instead of array, use next format:

[
  { name: 'Root', email: '[email protected]', groups: {role: 'root', countries: :all}, password: 'asdfasdf' },
  { name: 'Admin', email: '[email protected]', groups: {role: 'admin', countries: :all}, password: 'asdfasdf' },
  { name: 'Dev', email: '[email protected]', groups: {role: 'dev', countries: :all}, password: 'asdfasdf' },
  { name: 'BO', email: '[email protected]', groups: {role: 'root', countries: :all}, password: 'asdfasdf' },
  { name: 'Affiliate', email: '[email protected]', groups: {role: 'affiliate', countries: 'uk', gateways: ['Oxygen8']}, password: 'asdfasdf' }
].each do |attributes|
  User.find_or_create_by(attributes)
end
Sign up to request clarification or add additional context in comments.

1 Comment

It worked! I fixed the syntax for you. Btw, thanks for the reply :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.