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: GitHub - davidpiegza/rails4-belongs-to-test.

Any ideas why it doesn’t work in Rails 4?

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 19 July 2013 11:32, David [email protected] wrote:

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

On 19 July 2013 14:59, David [email protected] 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

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

Colin L. wrote in post #1116001:

On 19 July 2013 14:59, David [email protected] 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.

Although that might have been unintentional. I don’t know of any
“official” reserved word list for association names.

On 19 July 2013 15:15, Robert W. [email protected] wrote:

“official” reserved word list for association names.
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

On Jul 19, 2013, at 11:41 AM, Colin L. wrote:

On 19 July 2013 15:24, David [email protected] wrote:

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

On 19 July 2013 17:02, Walter Lee D. [email protected] wrote:

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.

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] Call for help: Rails Reserved Words Wiki - Rails - Ruby-Forum

On 19 July 2013 15:24, David [email protected] wrote:

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

On Jul 19, 2013, at 7:15 AM, Robert W. [email protected] wrote:

“official” reserved word list for association names.

‘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.

On Jul 19, 2013, at 12:17 PM, Colin L. wrote:

Could I suggest you make the title “Reserved Words in Ruby on Rails”

Done and done.

Walter

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