Can you create and initialize an ActiveRecord object like this?
exercise = Exercise.new
exercise.name = "foo"
exercise.description = "bar"
exercise.unit_id = 123
I am trying to do this and I keep getting
ActiveRecord::ConnectionNotEstablished - ActiveRecord::ConnectionNotEstablished:
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
/usr/lib/ruby/1.8/active_record/base.rb:1271:in `columns'
/usr/lib/ruby/1.8/active_record/base.rb:3014:in `attributes_from_column_definition_without_lock'
/usr/lib/ruby/1.8/active_record/locking/optimistic.rb:55:in `attributes_from_column_definition'
/usr/lib/ruby/1.8/active_record/base.rb:2434:in `initialize'
main.rb:14:in `new'
I just want to create and initialize the object then call a .save method. If I am going about this incorrectly, please let me know.
Short Answer: Yes, you need a connection!
You cannot (generally) use ActiveRecord unless there is a connection.
The set of attributes on the object is determined by the columns on the table. Which means you can’t use the object unless you have the connection.
In any case what would happen after calling
exercise.saveif there is no connection?So if you only need the validation, you can reuse ActiveSupport::Validation on a transient class.