aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2013-01-02 05:07:32 +0900
committerAkira Matsuda <ronnie@dio.jp>2013-01-02 05:12:16 +0900
commit2565c81862a7dc5cc2ab058d7fb25759aab1af7b (patch)
tree04227c76519baf01d36e0460be7b8fd2f13867a6 /guides/source
parent44717a9d548c95c7b01e7e0dce061257b3a93646 (diff)
downloadrails-2565c81862a7dc5cc2ab058d7fb25759aab1af7b.tar.gz
rails-2565c81862a7dc5cc2ab058d7fb25759aab1af7b.tar.bz2
rails-2565c81862a7dc5cc2ab058d7fb25759aab1af7b.zip
find_or_create_by is deprecated in AR 4
Diffstat (limited to 'guides/source')
-rw-r--r--guides/source/engines.md10
1 files changed, 5 insertions, 5 deletions
diff --git a/guides/source/engines.md b/guides/source/engines.md
index 116a7e67cd..d5ec2658fe 100644
--- a/guides/source/engines.md
+++ b/guides/source/engines.md
@@ -531,7 +531,7 @@ before_save :set_author
private
def set_author
- self.author = User.find_or_create_by_name(author_name)
+ self.author = User.find_or_create_by(name: author_name)
end
```
@@ -630,7 +630,7 @@ belongs_to :author, class_name: Blorgh.user_class
The `set_author` method also located in this class should also use this class:
```ruby
-self.author = Blorgh.user_class.constantize.find_or_create_by_name(author_name)
+self.author = Blorgh.user_class.constantize.find_or_create_by(name: author_name)
```
To save having to call `constantize` on the `user_class` result all the time, you could instead just override the `user_class` getter method inside the `Blorgh` module in the `lib/blorgh.rb` file to always call `constantize` on the saved value before returning the result:
@@ -644,7 +644,7 @@ end
This would then turn the above code for `set_author` into this:
```ruby
-self.author = Blorgh.user_class.find_or_create_by_name(author_name)
+self.author = Blorgh.user_class.find_or_create_by(name: author_name)
```
Resulting in something a little shorter, and more implicit in its behaviour. The `user_class` method should always return a `Class` object.
@@ -661,7 +661,7 @@ WARNING: It's very important here to use the `String` version of the class, rath
Go ahead and try to create a new post. You will see that it works exactly in the same way as before, except this time the engine is using the configuration setting in `config/initializers/blorgh.rb` to learn what the class is.
-There are now no strict dependencies on what the class is, only what the API for the class must be. The engine simply requires this class to define a `find_or_create_by_name` method which returns an object of that class to be associated with a post when it's created. This object, of course, should have some sort of identifier by which it can be referenced.
+There are now no strict dependencies on what the class is, only what the API for the class must be. The engine simply requires this class to define a `find_or_create_by` method which returns an object of that class to be associated with a post when it's created. This object, of course, should have some sort of identifier by which it can be referenced.
#### General engine configuration
@@ -800,7 +800,7 @@ module Blorgh::Concerns::Models::Post
private
def set_author
- self.author = User.find_or_create_by_name(author_name)
+ self.author = User.find_or_create_by(name: author_name)
end
end