Neil Williams
2013-Oct-21 10:28 UTC
Strange behaviour with ActiveRecord has_many collections, Rails 4 compared to Rails 3
Hi All, I have been told to post discussion based topic on here instead of the GitHub Issues. I thought this ''could'' be a bug, but just need some clarification really. The issue I posted is here <https://github.com/rails/rails/issues/12597>, does anyone have any thoughts? Again: https://github.com/rails/rails/issues/12597 Thanks a lot. -- 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/91c5d7e3-ab38-45be-916a-89b6ff9de579%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Oct-21 12:23 UTC
Re: Strange behaviour with ActiveRecord has_many collections, Rails 4 compared to Rails 3
On Monday, October 21, 2013 11:28:22 AM UTC+1, Neil Williams wrote:> > Hi All, > > I have been told to post discussion based topic on here instead of the > GitHub Issues. > > I thought this ''could'' be a bug, but just need some clarification really. > The issue I posted is here <https://github.com/rails/rails/issues/12597>, > does anyone have any thoughts? > > Again: https://github.com/rails/rails/issues/12597 > > Thanks a lot. >To be honest it feels to me like what you were using just happened to work by accident, because by default method_missing on the association sets up a scope and calls the method on the class. If this was indeed accidental behaviour then the change could well be the side effect of another change. Using new in this way isn''t something I''ve come across in quite a few years of using rails. Fred -- 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/3f7c34b5-4bb6-4ce9-9f61-8a87e4238d46%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Neil Williams
2013-Oct-21 15:37 UTC
Re: Strange behaviour with ActiveRecord has_many collections, Rails 4 compared to Rails 3
Hi Fred, Thanks for your reply. "Using new in this way isn''t something I''ve come across in quite a few years of using rails." How would you initialise a new Task object given you have the @story instance available to you? @task = Task.new(:story_id => @story.id) ? If the above, I really don''t see the point of the new and build methods for a has_many association... For a has_one association, I would be able to do: @task = @story.build_task So it makes no sense, that @task = @story.tasks.build doesn''t do the same thing... As ''build'' seems to just be an alias of ''new''. Thanks. On Monday, 21 October 2013 13:23:54 UTC+1, Frederick Cheung wrote:> > > > On Monday, October 21, 2013 11:28:22 AM UTC+1, Neil Williams wrote: >> >> Hi All, >> >> I have been told to post discussion based topic on here instead of the >> GitHub Issues. >> >> I thought this ''could'' be a bug, but just need some clarification really. >> The issue I posted is here <https://github.com/rails/rails/issues/12597>, >> does anyone have any thoughts? >> >> Again: https://github.com/rails/rails/issues/12597 >> >> Thanks a lot. >> > > To be honest it feels to me like what you were using just happened to work > by accident, because by default method_missing on the association sets up a > scope and calls the method on the class. If this was indeed accidental > behaviour then the change could well be the side effect of another change. > Using new in this way isn''t something I''ve come across in quite a few years > of using rails. > > Fred >-- 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/a2da6d12-383d-48d4-bf41-3306ce2c7e5e%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Oct-22 15:26 UTC
Re: Strange behaviour with ActiveRecord has_many collections, Rails 4 compared to Rails 3
On Monday, October 21, 2013 4:37:22 PM UTC+1, Neil Williams wrote:> > Hi Fred, > > Thanks for your reply. > > "Using new in this way isn''t something I''ve come across in quite a few > years of using rails." > How would you initialise a new Task object given you have the @story > instance available to you? > >I''ve always done @story.tasks.build(...) The fact that this adds the built instance to the in memory collection has never been a problem for me Fred -- 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/714198ae-6b69-4595-a29c-e9862604a516%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Neil Williams
2013-Oct-22 15:47 UTC
Re: Strange behaviour with ActiveRecord has_many collections, Rails 4 compared to Rails 3
Hi Fred, Thanks for this. yes I can understand why it has never been a problem. It won''t be too much of a problem for us either, as the collection variable e.g. @tasks, would normally be filtered with .where() or be ordered with .order(), therefore, they would be different instances. But I logged it as a bug, because it just doesn''t seem right. I can''t think of any reason why one would want the built instance added to the in memory collection... Anyway, I think I''ve been a bit cheeky on the Github issue, not sure if I''m going to get another response! Thanks for your reply though. On Tuesday, 22 October 2013 16:26:15 UTC+1, Frederick Cheung wrote:> > > > On Monday, October 21, 2013 4:37:22 PM UTC+1, Neil Williams wrote: >> >> Hi Fred, >> >> Thanks for your reply. >> >> "Using new in this way isn''t something I''ve come across in quite a few >> years of using rails." >> How would you initialise a new Task object given you have the @story >> instance available to you? >> >> > I''ve always done @story.tasks.build(...) > > The fact that this adds the built instance to the in memory collection has > never been a problem for me > > Fred >-- 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/3003a205-8b6d-493e-a7d5-3a894a17636e%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Maybe Matching Threads
- CRUD pattern for has_many relationships (forms containing collections)?
- how to use activemodel collection.build for a has_many :through association
- Avoiding adding duplicates to has_many collection?
- has_many.collection<<obj doesn''t set attributes?
- Deleting from a 'has_many' collection doesn't set the foreign key to NULL