Menu Close



Csvbuilder::Dynamic::Columns::Importer is part of the csvbuilder-collection

The Dynamic Columns Importer contains the implementation for importing CSV data with a variable group, for instance, an object with categories.


Install the gem and add to the application’s Gemfile by executing:

$ bundle add csvbuilder-dynamic-columns-importer

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install csvbuilder-dynamic-columns-importer


Let’s consider a Developer with languages skill.

class UserCsvImportModel
  include Csvbuilder::Model
  include Csvbuilder::Import

  column :name, header: "Developer"

  dynamic_column :skills

The Row can iterate over the Dynamic Columns. Here are skills, and get access to the cell values.

For each entry of the Dynamic Columns, here skills, a method called by the singular version name of the declared Dynamic Columns will be called here skill.

This method provides the header value and the cell value; however, it returns only the cell value by default; if that is all that is needed, there is no need for overriding.

However, it is safe to override this method to put in place any logic needed.

For instance, returning the header and cell values to figure out what the importer has to do with them.

Check out the following example:

class UserCsvImportModel
  include Csvbuilder::Model
  include Csvbuilder::Import

  column :name, header: "Developer"

  dynamic_column :skills

  def user
    User.where(name: name).take

  def skill(value, header_value)
    { skill: header_value, has: value }

  ["Developer", "Ruby", "Python", "Javascript"],
  ["Bob"      ,    "1",      "0",          "1"],
options = {}, UserCsvImportModel, options).each do |row_model| row_model.skills.each do |skill_data| skill = Skill.find_or_create_by(name: skill_data[:skill]) row_model.user.skills << skill if skill_data[:has] == "1" end end User.where(name: "Bob").take.skills.pluck(:name) # => ["Ruby", "Javascript"]


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Csvbuilder::Dynamic::Columns project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

View Source Code
Posted in Development