2

I'm running 3.2.13 and following the standard "railstutorial.org" I've run into a question. I'm still getting the hang of switching from a C++/Java programming mindset, and trying to understand normal calling conventions.

I can use my class (User < ActiveRecord::Base) like:

User.create(email: "email", password: "password", password_confirmation: "password")

The doc for the method indicates that it is deprecated. I also see no reference to a def create in my ActiveRecord gem source.

Since the call is not using a Hash, what is the name of that kind of invocation? Is it the same if the arguments were wrapped in curly brackets?

Substantial
  • 6,684
  • 2
  • 31
  • 40
chrisp
  • 2,181
  • 4
  • 27
  • 35
  • 1
    From what I understand it was just moved from `ActiveRecord::Base` to `ActiveResource::Base`. See http://apidock.com/rails/ActiveResource/Base/create/class And that is still a `Hash` but with the new Ruby 1.9 syntax. – Kleber S. Apr 10 '13 at 23:54
  • I was thinking that too... but I can't find the "line" from ActiveRecord to ActiveResource. My basic understand has me failing to uncover an ActiveResource "import" from ActiveRecord. – chrisp Apr 11 '13 at 00:47
  • ActiveResource is a totally different thing!!! ActiveResource is to interact with REST services but the API is similar to ActiveRecord. It was removed from rails and now you must add it to your gems to use it. – Ismael Abreu Apr 11 '13 at 00:53

2 Answers2

4

Two questions are being asked here.

First question: method deprecation

Changes were made in this commit.

The create method was removed from ActiveRecord::Base and inserted into a separate module called ActiveRecord::Persistence.

Regarding what APIDock told you... the method was "deprecated or moved." If you look halfway down the list of suggestions, you will see ActiveRecord::Persistence#create. This is where the method moved to.

Note that ActiveRecord::Persistence#create is used internally. When calling create on an ActiveRecord model (not object) in your application code, you are invoking ActiveRecord::Relation#create.

ActiveResource::Base#create was introduced in Rails 2.0 and is unrelated to ActiveRecord.

Second question: hash as arguments

Examining the source for ActiveRecord::Relation#create shows the following:

# File activerecord/lib/active_record/relation.rb, line 85
def create(*args, &block)
  scoping { @klass.create(*args, &block) }
end

See the splat argument *args? This tells Ruby to pass all remaining arguments into args, no matter how many there are. Ruby/Rails' ducktyping magic sees a bunch of key-value pairs and assumes it to be a hash.

Rails is a bit loose with requiring curly brackets for hashes in arguments, just as it is loose about wrapping all arguments in parentheses. Generally, Rails will infer a hash when you pass in a series of key-value pairs. Sometimes this causes trouble when passing in multiple hashes, like in a complex form_for method.

If you have erratic behavior with curly brackets absent, insert them. Clearly defining hashes will allow you to ensure proper behavior.

Read more about Ruby splats here.

Community
  • 1
  • 1
Substantial
  • 6,684
  • 2
  • 31
  • 40
1

"Deprecated" doesn't mean "no longer available". It means it will be no longer available soon.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • I do know what "deprecated" means... that's why I mentioned that I was not able to find the definition in my installed gem's source. :D The doc says "This method is deprecated or moved on the latest stable version. The last existing version (v3.1.0) is shown here.". – chrisp Apr 11 '13 at 00:47
  • +1. Deprecation means something is discontinued and will be removed in the future, so you shouldn't implement it, or should replace it if already implemented. – Substantial Apr 11 '13 at 00:47
  • Deprecation definition and meaning understood!!! Now where is the replacement for ActiveRecord::Base#create ? :) – chrisp Apr 11 '13 at 00:51