0

I'm trying to insert some options into the database using the terminal since there's no real admin for these options as they'll never change. I'm trying to use the following command

o = MeasurementOption.new(:name => 'Lbs', measurement_type_id => '3')

and I get the following error

syntax error near unexpected token `('

I'm looking at examples, and it seems like I have the syntax correct.

2 Answers 2

3

I think you are missing a colon

o = MeasurementOption.new(:name => 'Lbs', :measurement_type_id => '3')
                                          ^
Sign up to request clarification or add additional context in comments.

1 Comment

You may also want to consider putting this in your db/seeds.rb file. That way it'll automatically be created wherever you created the app. I didn't know about this useful option when I was first learning rails. You just put the statements you want (including save) in that file and then they become part of the db:drop db:create db:migrate db:seeds process.
1

The new standard way of doing this would be to lose the =>. It would be best to get into the Rails Way of doing it this way:

o = MeasurementOption.new(name: 'Lbs', measurement_type_id: '3')

Also, if you are adding some static values that will never change, it would hurt to create these in your seeds file.

Go to db/seeds.rb and add

o = MeasurementOption.create!(name: 'Lbs', measurement_type_id: 3)

Then you can also use o' later in the seeds file if you needed, like:

duplicate_option = o

Then whenever you wish to seed your database, you would simply call rake db:seed. This way you won't have to create that static MeasurementOption each time you need to reset your database.

Comments

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.