Ruby on Rails
Friday, July 19, 2013
Hi,
I discovered a weird behavior when using a "belongs_to: record" association in Rails 4.
Given two models A and B:
class A < ActiveRecord::Base
belongs_to :record, class_name: 'B', foreign_key: 'b_id'
end
class B < ActiveRecord::Base
end
When creating A, it inserts a record in B and returns A with id of nil:
irb(main):001:0> A.create!
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "bs" DEFAULT VALUES
(2.4ms) commit transaction
=> #<A id: nil, b_id: 1>
A.count # => 0
B.count # => 1
It used to work in Rails 3.
I've created a test repo for this: https://github.com/davidpiegza/rails4-belongs-to-test.
Any ideas why it doesn't work in Rails 4?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8387c4fb-9d0a-4636-8d78-8dd2de060dbc%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment