Pull Activerecord join table information through controller query











up vote
0
down vote

favorite












I have a query that generated a list of product data; I am working to dig in a bit deeper and extract an attribute from a join table (ProductSelections). Both RFIDTag and Products have many of the Product Selections model (i.e. the schema table contains both an rfid_tag_id and a product_id). Since I'm working from the product list, I'm deriving my query from here:



#original query (works as needed):

@warehoused_products = Product.includes(:rfid_tags).joins(:rfid_tags).where("rfid_tags.location_type = 'Warehouse' AND rfid_tags.location_id = ?", params[:id])

#Attempt at getting product selection data:

@product_histories = @warehoused_products.joins("INNER JOIN product_selections ON :product_selections.product_id = products.id").includes(:product_selections)


This raises a fairly large postgreSQL error when I check it:



raise @product_histories.inspect:

PG::SyntaxError: ERROR: syntax error at or near ":" LINE 1: ...eted_at" IS NULL INNER JOIN product_selections ON :product_s...


On adjusting the query:



raise @warehoused_products.includes(:product_selections).joins(:product_selections).where("product_selections.product_id = ?", params[:product_id]).first.inspect


I get a a nil result - error-free, and likely cleaner, but not quite there.



A Product Selection object looks like so, for reference:



#<ProductSelection id: 269, user_id: 2, listing_id: 11, product_id: 35, created_at: "2016-07-14 15:38:11", updated_at: "2016-08-08 21:10:13", rfid_tag_id: 575, part_id: nil, use_time: 2173274, account_id: 1, deleted_at: nil>


How can I resolve the query so that I can pull the associated table object?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a query that generated a list of product data; I am working to dig in a bit deeper and extract an attribute from a join table (ProductSelections). Both RFIDTag and Products have many of the Product Selections model (i.e. the schema table contains both an rfid_tag_id and a product_id). Since I'm working from the product list, I'm deriving my query from here:



    #original query (works as needed):

    @warehoused_products = Product.includes(:rfid_tags).joins(:rfid_tags).where("rfid_tags.location_type = 'Warehouse' AND rfid_tags.location_id = ?", params[:id])

    #Attempt at getting product selection data:

    @product_histories = @warehoused_products.joins("INNER JOIN product_selections ON :product_selections.product_id = products.id").includes(:product_selections)


    This raises a fairly large postgreSQL error when I check it:



    raise @product_histories.inspect:

    PG::SyntaxError: ERROR: syntax error at or near ":" LINE 1: ...eted_at" IS NULL INNER JOIN product_selections ON :product_s...


    On adjusting the query:



    raise @warehoused_products.includes(:product_selections).joins(:product_selections).where("product_selections.product_id = ?", params[:product_id]).first.inspect


    I get a a nil result - error-free, and likely cleaner, but not quite there.



    A Product Selection object looks like so, for reference:



    #<ProductSelection id: 269, user_id: 2, listing_id: 11, product_id: 35, created_at: "2016-07-14 15:38:11", updated_at: "2016-08-08 21:10:13", rfid_tag_id: 575, part_id: nil, use_time: 2173274, account_id: 1, deleted_at: nil>


    How can I resolve the query so that I can pull the associated table object?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a query that generated a list of product data; I am working to dig in a bit deeper and extract an attribute from a join table (ProductSelections). Both RFIDTag and Products have many of the Product Selections model (i.e. the schema table contains both an rfid_tag_id and a product_id). Since I'm working from the product list, I'm deriving my query from here:



      #original query (works as needed):

      @warehoused_products = Product.includes(:rfid_tags).joins(:rfid_tags).where("rfid_tags.location_type = 'Warehouse' AND rfid_tags.location_id = ?", params[:id])

      #Attempt at getting product selection data:

      @product_histories = @warehoused_products.joins("INNER JOIN product_selections ON :product_selections.product_id = products.id").includes(:product_selections)


      This raises a fairly large postgreSQL error when I check it:



      raise @product_histories.inspect:

      PG::SyntaxError: ERROR: syntax error at or near ":" LINE 1: ...eted_at" IS NULL INNER JOIN product_selections ON :product_s...


      On adjusting the query:



      raise @warehoused_products.includes(:product_selections).joins(:product_selections).where("product_selections.product_id = ?", params[:product_id]).first.inspect


      I get a a nil result - error-free, and likely cleaner, but not quite there.



      A Product Selection object looks like so, for reference:



      #<ProductSelection id: 269, user_id: 2, listing_id: 11, product_id: 35, created_at: "2016-07-14 15:38:11", updated_at: "2016-08-08 21:10:13", rfid_tag_id: 575, part_id: nil, use_time: 2173274, account_id: 1, deleted_at: nil>


      How can I resolve the query so that I can pull the associated table object?










      share|improve this question















      I have a query that generated a list of product data; I am working to dig in a bit deeper and extract an attribute from a join table (ProductSelections). Both RFIDTag and Products have many of the Product Selections model (i.e. the schema table contains both an rfid_tag_id and a product_id). Since I'm working from the product list, I'm deriving my query from here:



      #original query (works as needed):

      @warehoused_products = Product.includes(:rfid_tags).joins(:rfid_tags).where("rfid_tags.location_type = 'Warehouse' AND rfid_tags.location_id = ?", params[:id])

      #Attempt at getting product selection data:

      @product_histories = @warehoused_products.joins("INNER JOIN product_selections ON :product_selections.product_id = products.id").includes(:product_selections)


      This raises a fairly large postgreSQL error when I check it:



      raise @product_histories.inspect:

      PG::SyntaxError: ERROR: syntax error at or near ":" LINE 1: ...eted_at" IS NULL INNER JOIN product_selections ON :product_s...


      On adjusting the query:



      raise @warehoused_products.includes(:product_selections).joins(:product_selections).where("product_selections.product_id = ?", params[:product_id]).first.inspect


      I get a a nil result - error-free, and likely cleaner, but not quite there.



      A Product Selection object looks like so, for reference:



      #<ProductSelection id: 269, user_id: 2, listing_id: 11, product_id: 35, created_at: "2016-07-14 15:38:11", updated_at: "2016-08-08 21:10:13", rfid_tag_id: 575, part_id: nil, use_time: 2173274, account_id: 1, deleted_at: nil>


      How can I resolve the query so that I can pull the associated table object?







      ruby-on-rails ruby join activerecord controller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 5:34

























      asked Nov 19 at 22:42









      Boucherie

      296213




      296213
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          @warehoused_products = Product.includes(:rfid_tags).where("rfid_tags.location_type = ? AND rfid_tags.location_id = ?", 'Warehouse', params[:id]) 


          @product_histories = @warehoused_products.includes("product_selections").where("product_selections.product_id = ?", params[:product_id]).first.inspect





          share|improve this answer





















          • That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
            – Boucherie
            Nov 20 at 15:08













          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',
          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%2f53383703%2fpull-activerecord-join-table-information-through-controller-query%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








          up vote
          0
          down vote













          @warehoused_products = Product.includes(:rfid_tags).where("rfid_tags.location_type = ? AND rfid_tags.location_id = ?", 'Warehouse', params[:id]) 


          @product_histories = @warehoused_products.includes("product_selections").where("product_selections.product_id = ?", params[:product_id]).first.inspect





          share|improve this answer





















          • That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
            – Boucherie
            Nov 20 at 15:08

















          up vote
          0
          down vote













          @warehoused_products = Product.includes(:rfid_tags).where("rfid_tags.location_type = ? AND rfid_tags.location_id = ?", 'Warehouse', params[:id]) 


          @product_histories = @warehoused_products.includes("product_selections").where("product_selections.product_id = ?", params[:product_id]).first.inspect





          share|improve this answer





















          • That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
            – Boucherie
            Nov 20 at 15:08















          up vote
          0
          down vote










          up vote
          0
          down vote









          @warehoused_products = Product.includes(:rfid_tags).where("rfid_tags.location_type = ? AND rfid_tags.location_id = ?", 'Warehouse', params[:id]) 


          @product_histories = @warehoused_products.includes("product_selections").where("product_selections.product_id = ?", params[:product_id]).first.inspect





          share|improve this answer












          @warehoused_products = Product.includes(:rfid_tags).where("rfid_tags.location_type = ? AND rfid_tags.location_id = ?", 'Warehouse', params[:id]) 


          @product_histories = @warehoused_products.includes("product_selections").where("product_selections.product_id = ?", params[:product_id]).first.inspect






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:05









          Gabbar

          3,8431317




          3,8431317












          • That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
            – Boucherie
            Nov 20 at 15:08




















          • That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
            – Boucherie
            Nov 20 at 15:08


















          That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
          – Boucherie
          Nov 20 at 15:08






          That's the second thing I tried - I get nil from this (this version makes it back into an instance variable, but still the same)
          – Boucherie
          Nov 20 at 15:08




















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53383703%2fpull-activerecord-join-table-information-through-controller-query%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin