Category: Rails

Bundler warning: this Gemfile contains multiple primary sources

If you see this error:

Warning: this Gemfile contains multiple primary sources. Using `source` more than once without a block is a security risk, and may result in installing unexpected gems. To resolve this warning, use a block to indicate which gems should come from the secondary source. To upgrade this warning to an error, run `bundle config disable_multisource true`.

It means that you have more than one source in your Gemfile. For example if you use Rails Assets, it might look like this:

source 'https://rubygems.org'
source 'https://rails-assets.org'

# Gems list here...

It is unsafe, because the second source could "inject" a different version of your gems.

To prevent this, you need to declare which gems you want from the second source:

source 'https://rails-assets.org' do
  %w(
    jquery jquery-ujs bootstrap jquery-icheck select2 zeroclipboard
    font-awesome modernizer dropzone seiyria-bootstrap-slider jquery-masonry
    jquery-infinite-scroll imagesloaded markitup livestampjs datetimepicker
    videojs jquery.lazyload magnific-popup
  ).each do |asset_source|
    gem "rails-assets-#{asset_source}"
  end
end

ActiveRecord and FriendlyId: slug not generated when field set with before_validation callback

FriendlyId is an awesome gem for slugging and permalinking for Active Record. It works like a charm except one case: when you set friendly_id field in a before_validation callback.

Here's an example:

class User < ActiveRecord::Base
  extend FriendlyId

  friendly_id :login, use: %i( history finders )

  before_validation :set_login

  def set_login
    # This weird method will just generate a random string
    self.login = (0...8).map { (65 + rand(26)).chr }.join
  end
end

User.new.save!
User.last.login #=> 'bhkcjvrd'
User.last.to_param #=> user id
User.last.slug #=> ''

It happens because FriendlyId uses a before_validation callback to set a slug. This method is invoked before any before_validation callbacks that you define in your class.

To fix this you can either invoke friendly_id after you set your before_validation callbacks, or you can just prepend your method before friendly_id:

class User < ActiveRecord::Base
  extend FriendlyId

  friendly_id :login, use: %i( history finders )

  before_validation :set_login
    prepend: true

  def set_login
    # This weird method will just generate a random string
    self.login = (0...8).map { (65 + rand(26)).chr }.join
  end
end

User.new.save!
User.last.login #=> 'bhkcjvrd'
User.last.to_param #=> 'bhkcjvrd'
User.last.slug #=> 'bhkcjvrd'

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑