开发者

Ruby on Rails: Insert into database from the terminal

开发者 https://www.devze.com 2023-03-16 13:14 出处:网络
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

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.


I think you are missing a colon

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消