いよいよ明後日です。もうちょっとせなあかんことが残ってるような気もしますが、 まぁ何とかなるでしょう。是非ご参加ください。
new はブロックを取るのに、create は取らないんですねぇ。
tag = Tag.new{|t| t.name = "rails"}
tag.name #=> "rails"
tag = Tag.create{|t| t.name = "rails"}
tag.name #=> nil
こんな感じ?
Index: activerecord/lib/active_record/base.rb
===================================================================
--- activerecord/lib/active_record/base.rb (リビジョン 4780)
+++ activerecord/lib/active_record/base.rb (working copy)
@@ -439,11 +439,11 @@
# Creates an object, instantly saves it as a record (if the validation permits it), and returns it. If the save
# fails under validations, the unsaved object is still returned.
- def create(attributes = nil)
+ def create(attributes = nil, &block)
if attributes.is_a?(Array)
- attributes.collect { |attr| create(attr) }
+ attributes.collect { |attr| create(attr, &block) }
else
- object = new(attributes)
+ object = new(attributes, &block)
scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create)
object.save
object
@@ -1159,7 +1159,7 @@
#
# It's even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount
# is actually find_all_by_amount(amount, options).
- def method_missing(method_id, *arguments)
+ def method_missing(method_id, *arguments, &block)
if match = /find_(all_by|by)_([_a-zA-Z]\w*)/.match(method_id.to_s)
finder, deprecated_finder = determine_finder(match), determine_deprecated_finder(match)
@@ -1197,7 +1197,7 @@
options = { :conditions => construct_conditions_from_arguments(attribute_names, arguments) }
set_readonly_option!(options)
- find_initial(options) || send(instantiator, construct_attributes_from_arguments(attribute_names, arguments))
+ find_initial(options) || send(instantiator, construct_attributes_from_arguments(attribute_names, arguments), &block)
else
super
end
やりたいことは、まだこの先にあるんだけど。