In Rails console how do I find records that below to a table











up vote
0
down vote

favorite












I have 3 models:




  1. Workspace (has many projects)

  2. Project (belongs to workspace and has many tasks)

  3. Task (belongs to project)


In the Rails console, I can do;



w = Workspace.find(1)
w.projects.all


This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?



I have tried:



w.projects.tasks.all 


but this doesn't work










share|improve this question
























  • You can use has_many :through association. Ref: guides.rubyonrails.org/…
    – Gokul M
    yesterday










  • @GokulM worked perfectly. Thanks
    – DRadnor
    yesterday















up vote
0
down vote

favorite












I have 3 models:




  1. Workspace (has many projects)

  2. Project (belongs to workspace and has many tasks)

  3. Task (belongs to project)


In the Rails console, I can do;



w = Workspace.find(1)
w.projects.all


This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?



I have tried:



w.projects.tasks.all 


but this doesn't work










share|improve this question
























  • You can use has_many :through association. Ref: guides.rubyonrails.org/…
    – Gokul M
    yesterday










  • @GokulM worked perfectly. Thanks
    – DRadnor
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 3 models:




  1. Workspace (has many projects)

  2. Project (belongs to workspace and has many tasks)

  3. Task (belongs to project)


In the Rails console, I can do;



w = Workspace.find(1)
w.projects.all


This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?



I have tried:



w.projects.tasks.all 


but this doesn't work










share|improve this question















I have 3 models:




  1. Workspace (has many projects)

  2. Project (belongs to workspace and has many tasks)

  3. Task (belongs to project)


In the Rails console, I can do;



w = Workspace.find(1)
w.projects.all


This returns all of the projects that belong to workspace with Id = 1. How do o find all of the tasks that belong to workspace with Id = 1?



I have tried:



w.projects.tasks.all 


but this doesn't work







ruby-on-rails






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Sergio Tulentsev

176k29286300




176k29286300










asked yesterday









DRadnor

31




31












  • You can use has_many :through association. Ref: guides.rubyonrails.org/…
    – Gokul M
    yesterday










  • @GokulM worked perfectly. Thanks
    – DRadnor
    yesterday


















  • You can use has_many :through association. Ref: guides.rubyonrails.org/…
    – Gokul M
    yesterday










  • @GokulM worked perfectly. Thanks
    – DRadnor
    yesterday
















You can use has_many :through association. Ref: guides.rubyonrails.org/…
– Gokul M
yesterday




You can use has_many :through association. Ref: guides.rubyonrails.org/…
– Gokul M
yesterday












@GokulM worked perfectly. Thanks
– DRadnor
yesterday




@GokulM worked perfectly. Thanks
– DRadnor
yesterday












2 Answers
2






active

oldest

votes

















up vote
0
down vote













In your workspace model use the below association:



has_many :tasks, through: :projects


Also, I recommend you to check the Rails guides






share|improve this answer




























    up vote
    0
    down vote













    As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.



     w.projects.tasks.all


    The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.



    Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.



    #1st solution
    project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
    #2nd solution
    project_tasks = w.projects.map {|project| project.tasks } # will return all tasks


    Hope it clears your query!!






    share|improve this answer





















      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%2f53349285%2fin-rails-console-how-do-i-find-records-that-below-to-a-table%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      In your workspace model use the below association:



      has_many :tasks, through: :projects


      Also, I recommend you to check the Rails guides






      share|improve this answer

























        up vote
        0
        down vote













        In your workspace model use the below association:



        has_many :tasks, through: :projects


        Also, I recommend you to check the Rails guides






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          In your workspace model use the below association:



          has_many :tasks, through: :projects


          Also, I recommend you to check the Rails guides






          share|improve this answer












          In your workspace model use the below association:



          has_many :tasks, through: :projects


          Also, I recommend you to check the Rails guides







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Abhilash Reddy

          927616




          927616
























              up vote
              0
              down vote













              As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.



               w.projects.tasks.all


              The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.



              Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.



              #1st solution
              project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
              #2nd solution
              project_tasks = w.projects.map {|project| project.tasks } # will return all tasks


              Hope it clears your query!!






              share|improve this answer

























                up vote
                0
                down vote













                As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.



                 w.projects.tasks.all


                The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.



                Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.



                #1st solution
                project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
                #2nd solution
                project_tasks = w.projects.map {|project| project.tasks } # will return all tasks


                Hope it clears your query!!






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.



                   w.projects.tasks.all


                  The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.



                  Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.



                  #1st solution
                  project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
                  #2nd solution
                  project_tasks = w.projects.map {|project| project.tasks } # will return all tasks


                  Hope it clears your query!!






                  share|improve this answer












                  As per the description mentioned in the post you need to find the task belongs to a particular project that in turn belongs to a particular workspace.



                   w.projects.tasks.all


                  The above line won't be working as w.projects would returning collection of ActiveRecord and for association to work you need an object.



                  Assuming you have written necessary association as mentioned by @Abhilash, below mentioned query would achieve would you need.



                  #1st solution
                  project_tasks = w.includes(:projects => [:tasks]) #eager-loading both project and tasks
                  #2nd solution
                  project_tasks = w.projects.map {|project| project.tasks } # will return all tasks


                  Hope it clears your query!!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Rohan

                  9641311




                  9641311






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349285%2fin-rails-console-how-do-i-find-records-that-below-to-a-table%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