Rails 4 "belongs_to: record" association doesn't work

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?

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

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?

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

Colin Law wrote in post #1116001:

It would be weird if there are some reserved words for association names… and they are not documented.

It may well be a reserved word in other situations also.
Unfortunately I do not know of any up to date reserved word list at
all. There was a list in wiki.rubyonrails.org but the wiki
disappeared a long time ago. Googling has not found anything up to
date.

Colin

It would be weird if there are some reserved words for association names...
and they are not documented.

It may well be reserved in other situations also. No reserved words
are documented as far as I know.

Colin

It would be weird if there are some reserved words for association names...
and they are not documented.

It may well be reserved in other situations also. No reserved words
are documented as far as I know.

I started one here: http://reservedwords.herokuapp.com

I scraped the Rails Wiki before it went entirely away, and I also found another list somewhere (maybe StackOverflow) and merged the two. Only two people ever signed up to add more words, so it kind of died on the vine. If you want to contribute, you are certainly welcome to do so.

Walter

Am I the only one to be continually embarrassed when google turns up
results showing that I should have known the answer already? It
appears that it was my suggestion that you started that list. [1]

I have made sure to bookmark the url now and if I find a new one will update it.

Could I suggest you make the title "Reserved Words in Ruby on Rails"
as my initial search included the word ruby so yours did not turn up.
I usually include ruby when searching for rails as it reduces the
number of rolling stock hits.

Colin

[1] https://www.ruby-forum.com/topic/3608697

Done and done.

Walter

'record' is often a reserved word in the underlying dbms, but I was pretty sure Rails (AR) protected this such as surrounding field names and other identifiers with back ticks. Not sure if other dbmses do this or not….

I can confirm that with Rails 4.0.0 the previously working "belongs_to
:record" started behaving strangely.