Re: [Rails] Rails 4 "belongs_to: record" association doesn't work
On 19 July 2013 14:59, David <piegza@workhub.com> wrote:
> Yes exactly. It's specific to the word 'record'. It works when I change it
> to :foo or :b (or anything else).
>
> Are there any reserved words for association names?
It would appear that the answer to that question is yes.
Colin
>
> On Friday, July 19, 2013 3:38:57 PM UTC+2, Colin Law wrote:
>>
>> On 19 July 2013 11:32, David <pie...@workhub.com> wrote:
>> > 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.
>>
>> In the subject line you indicate that problem is specific to using the
>> word 'record' with belongs_to. Is that correct? In other words do
>> you get the same error with belongs_to :foo, .....
>>
>> Colin
--
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/CAL%3D0gLuQxLTFeYC7EYuvLad7JxJoUqTkZ1CxJ73S7GSKVuhb9A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home