rails form: don't show child in form, if already has this child












0














Person.rb:



class Person < ApplicationRecord
has_many :skills
end


Skill.rb:



   class Skill < ApplicationRecord
belongs_to :person
validates_uniqueness_of :name, :scope => :person_id
end


I made skills nested in Routes.rb:



  resources :people do 
resources :skills
end
end


Now, when creating a new skill for the client in clients/1/skills/new I do not want to be able to select a skill that the client already has. How can it be done? Here is my current input field:



= f.select :skill_id, Skill.all.map{|c| [c.name, c.id]}, {include_blank: true}









share|improve this question



























    0














    Person.rb:



    class Person < ApplicationRecord
    has_many :skills
    end


    Skill.rb:



       class Skill < ApplicationRecord
    belongs_to :person
    validates_uniqueness_of :name, :scope => :person_id
    end


    I made skills nested in Routes.rb:



      resources :people do 
    resources :skills
    end
    end


    Now, when creating a new skill for the client in clients/1/skills/new I do not want to be able to select a skill that the client already has. How can it be done? Here is my current input field:



    = f.select :skill_id, Skill.all.map{|c| [c.name, c.id]}, {include_blank: true}









    share|improve this question

























      0












      0








      0







      Person.rb:



      class Person < ApplicationRecord
      has_many :skills
      end


      Skill.rb:



         class Skill < ApplicationRecord
      belongs_to :person
      validates_uniqueness_of :name, :scope => :person_id
      end


      I made skills nested in Routes.rb:



        resources :people do 
      resources :skills
      end
      end


      Now, when creating a new skill for the client in clients/1/skills/new I do not want to be able to select a skill that the client already has. How can it be done? Here is my current input field:



      = f.select :skill_id, Skill.all.map{|c| [c.name, c.id]}, {include_blank: true}









      share|improve this question













      Person.rb:



      class Person < ApplicationRecord
      has_many :skills
      end


      Skill.rb:



         class Skill < ApplicationRecord
      belongs_to :person
      validates_uniqueness_of :name, :scope => :person_id
      end


      I made skills nested in Routes.rb:



        resources :people do 
      resources :skills
      end
      end


      Now, when creating a new skill for the client in clients/1/skills/new I do not want to be able to select a skill that the client already has. How can it be done? Here is my current input field:



      = f.select :skill_id, Skill.all.map{|c| [c.name, c.id]}, {include_blank: true}






      ruby-on-rails






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 19:25









      Yaro ShmYaro Shm

      12819




      12819
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I don't know which Rails version you're running, but you could make a scope for the Skill class.



           scope :remaining, ->(name) { where.not(name: name) }


          Then update your view:



           = f.select :skill_id, Skill.remaining(@person.skills.map(&:name)).map { |c| [c.name, c.id] }, { include_blank: true }


          That's untested, not sure if you'd need to .map the :name but you get the idea. Also change :name to :id if that suits better for your use case (I'm not sure if you enforce skill uniqueness across the board or just per-user).



          It looks a little clunky for your view, though, so you could throw it onto the Skill class assuming you have the right columns.



          def self.remaining # you're mapping the skill, not the client
          where.not(id: person.skill_ids).map { |s| [s.name, s.id] }
          end


          Then the view'd just be:



          = f.select :skill_id, Skill.remaining, { include_blank: true }


          As an aside, I'm going to guess the /clients versus /people are the same thing with different names only on SO, otherwise that's going to cause headaches and/or break.






          share|improve this answer































            0














            in person.rb



            def available_skills 
            skills = self.skills.pluck(:name, :id)
            Skill.pluck(:name, :id).select{|s| !skills.include?(s)}
            end


            in view template:



            <%= f.select :skill_id, @person.available_skills, {include_blank: true} %>





            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',
              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%2f53381324%2frails-form-dont-show-child-in-form-if-already-has-this-child%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









              0














              I don't know which Rails version you're running, but you could make a scope for the Skill class.



               scope :remaining, ->(name) { where.not(name: name) }


              Then update your view:



               = f.select :skill_id, Skill.remaining(@person.skills.map(&:name)).map { |c| [c.name, c.id] }, { include_blank: true }


              That's untested, not sure if you'd need to .map the :name but you get the idea. Also change :name to :id if that suits better for your use case (I'm not sure if you enforce skill uniqueness across the board or just per-user).



              It looks a little clunky for your view, though, so you could throw it onto the Skill class assuming you have the right columns.



              def self.remaining # you're mapping the skill, not the client
              where.not(id: person.skill_ids).map { |s| [s.name, s.id] }
              end


              Then the view'd just be:



              = f.select :skill_id, Skill.remaining, { include_blank: true }


              As an aside, I'm going to guess the /clients versus /people are the same thing with different names only on SO, otherwise that's going to cause headaches and/or break.






              share|improve this answer




























                0














                I don't know which Rails version you're running, but you could make a scope for the Skill class.



                 scope :remaining, ->(name) { where.not(name: name) }


                Then update your view:



                 = f.select :skill_id, Skill.remaining(@person.skills.map(&:name)).map { |c| [c.name, c.id] }, { include_blank: true }


                That's untested, not sure if you'd need to .map the :name but you get the idea. Also change :name to :id if that suits better for your use case (I'm not sure if you enforce skill uniqueness across the board or just per-user).



                It looks a little clunky for your view, though, so you could throw it onto the Skill class assuming you have the right columns.



                def self.remaining # you're mapping the skill, not the client
                where.not(id: person.skill_ids).map { |s| [s.name, s.id] }
                end


                Then the view'd just be:



                = f.select :skill_id, Skill.remaining, { include_blank: true }


                As an aside, I'm going to guess the /clients versus /people are the same thing with different names only on SO, otherwise that's going to cause headaches and/or break.






                share|improve this answer


























                  0












                  0








                  0






                  I don't know which Rails version you're running, but you could make a scope for the Skill class.



                   scope :remaining, ->(name) { where.not(name: name) }


                  Then update your view:



                   = f.select :skill_id, Skill.remaining(@person.skills.map(&:name)).map { |c| [c.name, c.id] }, { include_blank: true }


                  That's untested, not sure if you'd need to .map the :name but you get the idea. Also change :name to :id if that suits better for your use case (I'm not sure if you enforce skill uniqueness across the board or just per-user).



                  It looks a little clunky for your view, though, so you could throw it onto the Skill class assuming you have the right columns.



                  def self.remaining # you're mapping the skill, not the client
                  where.not(id: person.skill_ids).map { |s| [s.name, s.id] }
                  end


                  Then the view'd just be:



                  = f.select :skill_id, Skill.remaining, { include_blank: true }


                  As an aside, I'm going to guess the /clients versus /people are the same thing with different names only on SO, otherwise that's going to cause headaches and/or break.






                  share|improve this answer














                  I don't know which Rails version you're running, but you could make a scope for the Skill class.



                   scope :remaining, ->(name) { where.not(name: name) }


                  Then update your view:



                   = f.select :skill_id, Skill.remaining(@person.skills.map(&:name)).map { |c| [c.name, c.id] }, { include_blank: true }


                  That's untested, not sure if you'd need to .map the :name but you get the idea. Also change :name to :id if that suits better for your use case (I'm not sure if you enforce skill uniqueness across the board or just per-user).



                  It looks a little clunky for your view, though, so you could throw it onto the Skill class assuming you have the right columns.



                  def self.remaining # you're mapping the skill, not the client
                  where.not(id: person.skill_ids).map { |s| [s.name, s.id] }
                  end


                  Then the view'd just be:



                  = f.select :skill_id, Skill.remaining, { include_blank: true }


                  As an aside, I'm going to guess the /clients versus /people are the same thing with different names only on SO, otherwise that's going to cause headaches and/or break.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 '18 at 19:48

























                  answered Nov 19 '18 at 19:42









                  well-i-better-get-rollingwell-i-better-get-rolling

                  3,43432760




                  3,43432760

























                      0














                      in person.rb



                      def available_skills 
                      skills = self.skills.pluck(:name, :id)
                      Skill.pluck(:name, :id).select{|s| !skills.include?(s)}
                      end


                      in view template:



                      <%= f.select :skill_id, @person.available_skills, {include_blank: true} %>





                      share|improve this answer


























                        0














                        in person.rb



                        def available_skills 
                        skills = self.skills.pluck(:name, :id)
                        Skill.pluck(:name, :id).select{|s| !skills.include?(s)}
                        end


                        in view template:



                        <%= f.select :skill_id, @person.available_skills, {include_blank: true} %>





                        share|improve this answer
























                          0












                          0








                          0






                          in person.rb



                          def available_skills 
                          skills = self.skills.pluck(:name, :id)
                          Skill.pluck(:name, :id).select{|s| !skills.include?(s)}
                          end


                          in view template:



                          <%= f.select :skill_id, @person.available_skills, {include_blank: true} %>





                          share|improve this answer












                          in person.rb



                          def available_skills 
                          skills = self.skills.pluck(:name, :id)
                          Skill.pluck(:name, :id).select{|s| !skills.include?(s)}
                          end


                          in view template:



                          <%= f.select :skill_id, @person.available_skills, {include_blank: true} %>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 '18 at 20:25









                          Mr.YMr.Y

                          475212




                          475212






























                              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%2f53381324%2frails-form-dont-show-child-in-form-if-already-has-this-child%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

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              Npm cannot find a required file even through it is in the searched directory