# Create user field with options

**URL:** https://meta.discourse.org/t/create-user-field-with-options/44427
**Category:** Development
**Created:** [18 مايو 2016، 6:23ص UTC](https://meta.discourse.org/t/create-user-field-with-options/44427 "2016-05-18T06:23:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![alexu](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@alexu](https://meta.discourse.org/u/alexu)
#### Post date: [18 مايو 2016، 6:23ص UTC](https://meta.discourse.org/t/create-user-field-with-options/44427/1 "2016-05-18T06:23:01Z")

</div>

Hi I am playing around with the imports scripts and I want to create a drop down user field. The **ning** script has an example of creating a text field which I have working, and I am able to add a dropdown w/ no options, but when I change the type to drop down and add an options attribute like so

```
     @interests_field = UserField.find_by_name("My interests5")
     unless @interests_field
       @interests_field = UserField.create(name: "My interests5", description: "Do you like stuff5?", field_type: "dropdown", editable: true, required: true, show_on_profile: true, show_on_user_card: true, options: ['a','b', 'c'] )
     end

```

I get this error:

.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active\_record/attribute\_assignment.rb:59:in `rescue in \_assign\_attribute’: unknown attribute ‘options’ for UserField. (ActiveRecord::UnknownAttributeError)

I am new to ruby and discourse but I think the relevant code is in

./app/controllers/admin/user\_fields\_controller.rb

There is also a file

./spec/controllers/admin/user\_fields\_controller\_spec.rb

which suggests that options should be passable somehow?

Thanks to anyone who can shed light on this mystery.

Specifically how can I add options programmatically when creating a userfield in a ruby script situation.

---

<div class="post-metadata">

### Author: ![alexu](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@alexu](https://meta.discourse.org/u/alexu)
#### Post date: [18 مايو 2016، 7:58م UTC](https://meta.discourse.org/t/create-user-field-with-options/44427/2 "2016-05-18T19:58:57Z")

</div>

Woohoo, I figured out a way thanks to a random code snippets found on the web.

In local development, run like so:

~/discourse\> rails runner script/import\_scripts/test.rb

# test.rb

```
require "mysql2"

require File.expand_path(File.dirname( __FILE__ ) + "/base.rb")

class ImportScripts::UserFieldCreator < ImportScripts::Base

  def initialize
    super
  end

  def execute
     @interests_field = UserField.find_by_name("My interests5")
     unless @interests_field
       @interests_field = UserField.create(name: "My interests5", description: "Do you like stuff5?", field_type: "text", editable: true, required: true, show_on_profile: true, show_on_user_card: true)
     end

     @interests_field = UserField.find_by_name("My interests7")
     unless @interests_field
       @interests_field = UserField.create(name: "My interests7", description: "Do you like stuff6?", field_type: "dropdown", editable: true, required: true, show_on_profile: true, show_on_user_card: true)
        UserFieldOption.create(user_field_id: @interests_field.id, value: 'not really')
        UserFieldOption.create(user_field_id: @interests_field.id, value: 'a bit')
        UserFieldOption.create(user_field_id: @interests_field.id, value: 'some')
        UserFieldOption.create(user_field_id: @interests_field.id, value: 'a lot')
     end
  end
end

if __FILE__ ==$0
  ImportScripts::UserFieldCreator.new.perform
end

```
