Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many: collection.build doesn't allow passing a block to set attributes

#476

Given:

class Foo < ActiveRecord::Base
  has_many :bars
end

class Bar < ActiveRecord::Base
end

It is possible to set attributes of a newly created Bar, using

@bar = Bar.new do |b|
  b.something = "whatever"
end
@bar = Bar.create do |b|
  b.something = "whatever"
end
@bar = Foo.bars.create do |b|
  b.something = "whatever"
end

However, this doesn't work:

@bar = Foo.bars.build do |b|
  b.something = "whatever"
end

Reported by Michael Trim · June 23rd, 2008 @ 08:49 PM

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Michael Trim
    Michael Trim

    Attached a patch with this functionality (implemented in the same way as for collection.create)

    June 23rd, 2008 @ 09:11 PM

  2. Michael Trim
  3. Pratik
    Pratik
    • State changed from new to incomplete
    • Tag set to activerecord, edge, enhancement, has_many, patch

    The patch needs tests.

    Thanks.

    June 27th, 2008 @ 01:48 PM

  4. Pratik
    Pratik
    • State changed from incomplete to duplicate

    July 4th, 2008 @ 02:10 AM