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

143 views
Skip to first unread message

David

unread,
Jul 19, 2013, 10:32:07 AM7/19/13
to rubyonra...@googlegroups.com
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?

Colin Law

unread,
Jul 19, 2013, 1:38:57 PM7/19/13
to rubyonra...@googlegroups.com
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

David

unread,
Jul 19, 2013, 1:59:58 PM7/19/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
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?

Colin Law

unread,
Jul 19, 2013, 2:06:03 PM7/19/13
to David, rubyonra...@googlegroups.com
On 19 July 2013 14:59, David <pie...@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

Robert Walker

unread,
Jul 19, 2013, 2:15:14 PM7/19/13
to rubyonra...@googlegroups.com
Colin Law wrote in post #1116001:
> On 19 July 2013 14:59, David <pie...@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.

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

--
Posted via http://www.ruby-forum.com/.

David

unread,
Jul 19, 2013, 2:24:58 PM7/19/13
to rubyonra...@googlegroups.com
It would be weird if there are some reserved words for association names... and they are not documented.

Colin Law

unread,
Jul 19, 2013, 3:40:48 PM7/19/13
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Jul 19, 2013, 3:41:39 PM7/19/13
to rubyonra...@googlegroups.com
On 19 July 2013 15:24, David <pie...@workhub.com> 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 Friday, July 19, 2013 4:15:14 PM UTC+2, Ruby-Forum.com User wrote:
>>
>> Colin Law wrote in post #1116001:
>> > On 19 July 2013 14:59, David <pie...@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.
>>
>> Although that might have been unintentional. I don't know of any
>> "official" reserved word list for association names.
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/0f83d777-343d-47b3-9dab-1bac391518b1%40googlegroups.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Walter Lee Davis

unread,
Jul 19, 2013, 4:02:30 PM7/19/13
to rubyonra...@googlegroups.com

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

> On 19 July 2013 15:24, David <pie...@workhub.com> 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
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLubtgQDX%2BuWMhksc3OMLsfomYw4YTaA80Prmgp%3DHLHbUQ%40mail.gmail.com.

Colin Law

unread,
Jul 19, 2013, 4:17:54 PM7/19/13
to rubyonra...@googlegroups.com
On 19 July 2013 17:02, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
> On Jul 19, 2013, at 11:41 AM, Colin Law wrote:
>
>> On 19 July 2013 15:24, David <pie...@workhub.com> 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.

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

Walter Lee Davis

unread,
Jul 19, 2013, 5:34:05 PM7/19/13
to rubyonra...@googlegroups.com

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

> Could I suggest you make the title "Reserved Words in Ruby on Rails"

Done and done.

Walter

Tamara Temple

unread,
Jul 19, 2013, 9:29:00 PM7/19/13
to rubyonra...@googlegroups.com
'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….


Daniele Orlandi

unread,
Sep 2, 2013, 3:02:53 PM9/2/13
to rubyonra...@googlegroups.com
I can confirm that with Rails 4.0.0 the previously working "belongs_to
:record" started behaving strangely.
Reply all
Reply to author
Forward
0 new messages