복붙노트

[RUBY-ON-RAILS] 테이블을 조인 명명 규칙 레일

RUBY-ON-RAILS

테이블을 조인 명명 규칙 레일

레일 테이블을 조인하는 방법 링크 형태로 만든 후이 질문에서 유래

나는 내 제품 및 카테고리 모델 사이에 테이블을 조인 만드는 오전.

조인 테이블은 무엇을 이름을 지정해야합니다? categories_products 또는 category_products 또는 뭔가?

해결법

  1. ==============================

    1.categories_products. 모두 복수. 어휘 위해.

    categories_products. 모두 복수. 어휘 위해.

    인용문:

  2. ==============================

    2.레일 4에서 몇 가지 새로운 규칙이 있음을주의하십시오.

    레일 4에서 몇 가지 새로운 규칙이 있음을주의하십시오.

    레일 용 v4.2.7 => 오피스

    # alphabetically order
    developers + projects                 -->  developers_projects 
    
    # precedence is calculated with '<', lengthier strings have precedence 
    # if the string are equal compared to the shortest length
    paper_boxes + papers                  -->  paper_boxes_papers  
    
    # common prefix omitted
    catalog_categories + catalog_products -->  catalog_categories_products 
    

    규칙은 여전히 ​​꽤 동일합니다. 레일 (5)으로 우리는 창조를위한 새로운 도우미 마이그레이션과 테이블을 조인 있습니다 :

    class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration[5.0]
      def change
        create_join_table :developers, :projects
      end
    end
    

    레일 => 에지 문서

  3. ==============================

    3.레일 가입 테이블은 알파벳 순으로 만 작성해야합니다. 마음에 당신이 테이블을 조인을 만들 때마다이 점을 유지합니다.

    레일 가입 테이블은 알파벳 순으로 만 작성해야합니다. 마음에 당신이 테이블을 조인을 만들 때마다이 점을 유지합니다.

    당신이 만들려면 예를 들어, 프로젝트 테이블과 협력자 테이블 사이의 테이블을 조인 당신은 다음과 같은 이름을 지정해야합니다.

    구문 : first_table_name (밑줄) second_table_name

    # Names must be in alphabetical order and also in plural
    # Decide which is your first table name based on the alphabetical order
    

    예 : 프로젝트 및 공동 작업자 사이에 표를 가입 만들기

    Collaborator-Project
    
    collaborators_projects   
    # you should name it  like this; In alphabetical order with plural names
    

    예 2 : 블로그 게시물 테이블과 사용자 테이블 사이의 테이블에 참여 만들기

    BlogPost-User
    
    blog_posts_users      # In alphabetical order with plural names
    
  4. ==============================

    4.새로운 create_join_table 이전에는 이름 지정 규칙은 모델 이름이 필요하지 않습니다 그래서이, 아니 모델에 대응 한 테이블에 가입 만듭니다.

    새로운 create_join_table 이전에는 이름 지정 규칙은 모델 이름이 필요하지 않습니다 그래서이, 아니 모델에 대응 한 테이블에 가입 만듭니다.

    조인에 액세스하려면 생성 된 협회를 통해 두 테이블에 has_and_belongs_to_many 및 액세스를 선언해야합니다.

  5. from https://stackoverflow.com/questions/11590469/rails-naming-convention-for-join-table by cc-by-sa and MIT license