This project is archived and is in readonly mode.
ActiveRecord AssociationCollection build should set belongs_to association before calling block
Reported by 2 College Bums | June 23rd, 2010 @ 01:19 AM
activerecord/associations/association_collection.rb
Current implementation is this:
def build(attributes = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| build(attr, &block) }
else
build_record(attributes) do |record|
block.call(record) if block_given?
set_belongs_to_association_for(record)
end
end
end
By implementing the build like this:
def build(attributes = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| build(attr, &block) }
else
build_record(attributes) do |record|
set_belongs_to_association_for(record)
block.call(record) if block_given?
end
end
end
The callback can link to the associated object.
an example usage is:
author.autobiographies.build do |book|
book.title = "#{book.author.name}'s autobiography"
end
Comments and changes to this ticket
-
Neeraj Singh June 23rd, 2010 @ 01:53 AM
Can you add a failing test and also provide patch as per the guidelines. This looks interesting.
-
Santiago Pastorino June 24th, 2010 @ 11:37 PM
Follow this http://rails.lighthouseapp.com/projects/8994/sending-patches to provide a test case and a patch ;)
-
Neeraj Singh September 8th, 2010 @ 09:39 PM
- State changed from new to invalid
- Importance changed from to Low
Even with existing code you can do
author.autobiographies.build do |book| book.title = "#{book.author.name}'s autobiography" end
and it will work.
-
Rohit Arondekar October 7th, 2010 @ 05:21 AM
- Tag changed from activerecord associations, activerecord, associations, build to activerecord, associations, build
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>