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? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8387c4fb-9d0a-4636-8d78-8dd2de060dbc%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Jul-19 13:38 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
On 19 July 2013 11:32, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuVmWbMtYEw-Wnm1mDPgNK3EDefh-bSSRZqrA7_HE82Ew%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
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...-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org <javascript:>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Colin Law
2013-Jul-19 14:06 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
On 19 July 2013 14:59, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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...-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Robert Walker
2013-Jul-19 14:15 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
Colin Law wrote in post #1116001:> On 19 July 2013 14:59, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2e16d60ddc201bb0feebb1f26f17e76d%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
It would be weird if there are some reserved words for association names... and they are not documented. 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...-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org <javascript:>> 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Colin Law
2013-Jul-19 15:40 UTC
Re: Re: Rails 4 "belongs_to: record" association doesn''t work
On 19 July 2013 15:15, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1116001: >> On 19 July 2013 14:59, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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.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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvaKVT3i4%2BEKfZ2siCbPAp2Xy4UiEMJe1DQ7UtxXvvBQQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Jul-19 15:41 UTC
Re: Re: Rails 4 "belongs_to: record" association doesn''t work
On 19 July 2013 15:24, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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...-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLubtgQDX%2BuWMhksc3OMLsfomYw4YTaA80Prmgp%3DHLHbUQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jul-19 16:02 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
On Jul 19, 2013, at 11:41 AM, Colin Law wrote:> On 19 July 2013 15:24, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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> > 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...-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> 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. >> >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLubtgQDX%2BuWMhksc3OMLsfomYw4YTaA80Prmgp%3DHLHbUQ%40mail.gmail.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/144B9C5E-8105-4446-88FF-F727FC82EB59%40wdstudio.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Jul-19 16:17 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
On 19 July 2013 17:02, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> > On Jul 19, 2013, at 11:41 AM, Colin Law wrote: > >> On 19 July 2013 15:24, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuEb4SKUZtr_qPTN6rSRsheps5FDPhW1AuW8uHG2hOKXw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jul-19 17:34 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/E11456BE-4D7A-4B8E-8716-52E983455F6B%40wdstudio.com. For more options, visit https://groups.google.com/groups/opt_out.
Tamara Temple
2013-Jul-19 21:29 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
On Jul 19, 2013, at 7:15 AM, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1116001: >> On 19 July 2013 14:59, David <piegza-8hzWVIvsJapBDgjK7y7TUQ@public.gmane.org> 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. >''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…. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/02AB5116-BF44-480A-8026-9EBD29A1ED58%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Daniele Orlandi
2013-Sep-02 15:02 UTC
Re: Rails 4 "belongs_to: record" association doesn''t work
I can confirm that with Rails 4.0.0 the previously working "belongs_to :record" started behaving strangely. -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cf74fda421835ad3739a04ce5a8c1742%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.