This project is archived and is in readonly mode.

#476 ✓duplicate
Michael Trim

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

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

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

Comments and changes to this ticket

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>

People watching this ticket

Attachments

Pages