Friday, July 19, 2013

Re: [Rails] Rails 4 "belongs_to: record" association doesn't work

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?

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/548848bd-7315-497b-b2cd-796770ef66b7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate