How do you create many-to-many class instance relationships in JRuby w/ActiveRecord?












0















I have this basic setup:



class Foo < ActiveRecord::Base
self.primary_key = 'foo_id'
has_and_belongs_to_many :bars
end

class Bar < ActiveRecord::Base
self.primary_key = :bar_id
has_and_belongs_to_many :foos
end


Now I can see all the bars associated with foos using Foo.first.bars or Bar.first.foos and this works as expected.



Where I'm stumped is how to do something like this:



foo_rows = Foo.all
=> (all those rows)
bar_rows = Bar.all
=> (all those rows)
foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
=> "The value from the database"
bar_rows.find { |bar| bar.bar_id == 1 }.some_col = 'a new value'
=> "a new value"
foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
=> "a new value"


But instead that last line says "The value from the database"



How do I achieve the desired behaviour?










share|improve this question



























    0















    I have this basic setup:



    class Foo < ActiveRecord::Base
    self.primary_key = 'foo_id'
    has_and_belongs_to_many :bars
    end

    class Bar < ActiveRecord::Base
    self.primary_key = :bar_id
    has_and_belongs_to_many :foos
    end


    Now I can see all the bars associated with foos using Foo.first.bars or Bar.first.foos and this works as expected.



    Where I'm stumped is how to do something like this:



    foo_rows = Foo.all
    => (all those rows)
    bar_rows = Bar.all
    => (all those rows)
    foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
    => "The value from the database"
    bar_rows.find { |bar| bar.bar_id == 1 }.some_col = 'a new value'
    => "a new value"
    foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
    => "a new value"


    But instead that last line says "The value from the database"



    How do I achieve the desired behaviour?










    share|improve this question

























      0












      0








      0








      I have this basic setup:



      class Foo < ActiveRecord::Base
      self.primary_key = 'foo_id'
      has_and_belongs_to_many :bars
      end

      class Bar < ActiveRecord::Base
      self.primary_key = :bar_id
      has_and_belongs_to_many :foos
      end


      Now I can see all the bars associated with foos using Foo.first.bars or Bar.first.foos and this works as expected.



      Where I'm stumped is how to do something like this:



      foo_rows = Foo.all
      => (all those rows)
      bar_rows = Bar.all
      => (all those rows)
      foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
      => "The value from the database"
      bar_rows.find { |bar| bar.bar_id == 1 }.some_col = 'a new value'
      => "a new value"
      foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
      => "a new value"


      But instead that last line says "The value from the database"



      How do I achieve the desired behaviour?










      share|improve this question














      I have this basic setup:



      class Foo < ActiveRecord::Base
      self.primary_key = 'foo_id'
      has_and_belongs_to_many :bars
      end

      class Bar < ActiveRecord::Base
      self.primary_key = :bar_id
      has_and_belongs_to_many :foos
      end


      Now I can see all the bars associated with foos using Foo.first.bars or Bar.first.foos and this works as expected.



      Where I'm stumped is how to do something like this:



      foo_rows = Foo.all
      => (all those rows)
      bar_rows = Bar.all
      => (all those rows)
      foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
      => "The value from the database"
      bar_rows.find { |bar| bar.bar_id == 1 }.some_col = 'a new value'
      => "a new value"
      foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.some_col
      => "a new value"


      But instead that last line says "The value from the database"



      How do I achieve the desired behaviour?







      ruby activerecord jruby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 23:22









      squirrelchewsquirrelchew

      186




      186
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your bar_rows and foo_rows.first.bars are arrays with different objects in memory. Just because the id attribute of one of their elements is equal, it doesn't mean they're the same objects:



          bar_rows.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057500
          foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057123


          You are changing attribute of one of these objects, there is no reason that the attribute of the second object should be changed.



          As for the JRuby part, it does not matter -- MRI would behave the same.






          share|improve this answer
























          • Yeah, that explains the behaviour. Is there a way to get the desired effect?

            – squirrelchew
            Nov 22 '18 at 0:04













          • It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

            – Marcin Kołodziej
            Nov 22 '18 at 0:07











          • I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

            – squirrelchew
            Nov 22 '18 at 0:18













          • I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

            – Marcin Kołodziej
            Nov 22 '18 at 0:20











          • That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

            – squirrelchew
            Nov 22 '18 at 0:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421835%2fhow-do-you-create-many-to-many-class-instance-relationships-in-jruby-w-activerec%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your bar_rows and foo_rows.first.bars are arrays with different objects in memory. Just because the id attribute of one of their elements is equal, it doesn't mean they're the same objects:



          bar_rows.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057500
          foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057123


          You are changing attribute of one of these objects, there is no reason that the attribute of the second object should be changed.



          As for the JRuby part, it does not matter -- MRI would behave the same.






          share|improve this answer
























          • Yeah, that explains the behaviour. Is there a way to get the desired effect?

            – squirrelchew
            Nov 22 '18 at 0:04













          • It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

            – Marcin Kołodziej
            Nov 22 '18 at 0:07











          • I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

            – squirrelchew
            Nov 22 '18 at 0:18













          • I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

            – Marcin Kołodziej
            Nov 22 '18 at 0:20











          • That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

            – squirrelchew
            Nov 22 '18 at 0:47
















          0














          Your bar_rows and foo_rows.first.bars are arrays with different objects in memory. Just because the id attribute of one of their elements is equal, it doesn't mean they're the same objects:



          bar_rows.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057500
          foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057123


          You are changing attribute of one of these objects, there is no reason that the attribute of the second object should be changed.



          As for the JRuby part, it does not matter -- MRI would behave the same.






          share|improve this answer
























          • Yeah, that explains the behaviour. Is there a way to get the desired effect?

            – squirrelchew
            Nov 22 '18 at 0:04













          • It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

            – Marcin Kołodziej
            Nov 22 '18 at 0:07











          • I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

            – squirrelchew
            Nov 22 '18 at 0:18













          • I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

            – Marcin Kołodziej
            Nov 22 '18 at 0:20











          • That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

            – squirrelchew
            Nov 22 '18 at 0:47














          0












          0








          0







          Your bar_rows and foo_rows.first.bars are arrays with different objects in memory. Just because the id attribute of one of their elements is equal, it doesn't mean they're the same objects:



          bar_rows.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057500
          foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057123


          You are changing attribute of one of these objects, there is no reason that the attribute of the second object should be changed.



          As for the JRuby part, it does not matter -- MRI would behave the same.






          share|improve this answer













          Your bar_rows and foo_rows.first.bars are arrays with different objects in memory. Just because the id attribute of one of their elements is equal, it doesn't mean they're the same objects:



          bar_rows.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057500
          foo_rows.first.bars.find { |bar| bar.bar_id == 1 }.object_id
          # => 40057123


          You are changing attribute of one of these objects, there is no reason that the attribute of the second object should be changed.



          As for the JRuby part, it does not matter -- MRI would behave the same.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 23:56









          Marcin KołodziejMarcin Kołodziej

          4,4801315




          4,4801315













          • Yeah, that explains the behaviour. Is there a way to get the desired effect?

            – squirrelchew
            Nov 22 '18 at 0:04













          • It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

            – Marcin Kołodziej
            Nov 22 '18 at 0:07











          • I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

            – squirrelchew
            Nov 22 '18 at 0:18













          • I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

            – Marcin Kołodziej
            Nov 22 '18 at 0:20











          • That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

            – squirrelchew
            Nov 22 '18 at 0:47



















          • Yeah, that explains the behaviour. Is there a way to get the desired effect?

            – squirrelchew
            Nov 22 '18 at 0:04













          • It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

            – Marcin Kołodziej
            Nov 22 '18 at 0:07











          • I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

            – squirrelchew
            Nov 22 '18 at 0:18













          • I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

            – Marcin Kołodziej
            Nov 22 '18 at 0:20











          • That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

            – squirrelchew
            Nov 22 '18 at 0:47

















          Yeah, that explains the behaviour. Is there a way to get the desired effect?

          – squirrelchew
          Nov 22 '18 at 0:04







          Yeah, that explains the behaviour. Is there a way to get the desired effect?

          – squirrelchew
          Nov 22 '18 at 0:04















          It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

          – Marcin Kołodziej
          Nov 22 '18 at 0:07





          It's difficult to say what's your desired effect. You can either modify one of foo_rows.first.bars or modify the one from bar_rows, add it to foo_rows.first.bars and delete the previous one.

          – Marcin Kołodziej
          Nov 22 '18 at 0:07













          I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

          – squirrelchew
          Nov 22 '18 at 0:18







          I'd expect the bar with the id of 1 to be the same bar that's associated with all the foos, just as it would be in a relational database. This way I can locate a bar through a foo, update it right there, and not have to traverse every foo looking for that bar to update it as well.

          – squirrelchew
          Nov 22 '18 at 0:18















          I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

          – Marcin Kołodziej
          Nov 22 '18 at 0:20





          I don't think there's any way around it. You'll have to find the object directly in the associations of given foo. If you change the object somewhere else and want to have the changes reflected in foo, you'll have to reassign the associated item.

          – Marcin Kołodziej
          Nov 22 '18 at 0:20













          That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

          – squirrelchew
          Nov 22 '18 at 0:47





          That's what I was afraid of, hah! I guess I'll be writing some methods to do that all quick-like. Thanks!

          – squirrelchew
          Nov 22 '18 at 0:47




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421835%2fhow-do-you-create-many-to-many-class-instance-relationships-in-jruby-w-activerec%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]