복붙노트

[RUBY-ON-RAILS] 협회는 레일 협회에 아마 맞춤법이 틀린 문제를 발견하지 명명

RUBY-ON-RAILS

협회는 레일 협회에 아마 맞춤법이 틀린 문제를 발견하지 명명

여기 내 컨트롤러

@post = Post.joins(:customers).select("customers.*,posts.*").find params[:id]

내 게시물 모델

belongs_to :customer

내 고객 모델

has_many :posts

그리고 난 같은 오류를 얻고있다

Association named 'customers' was not found on Post; perhaps you misspelled it?

이것은 내 컨트롤러 출력 :

Processing by PostsController#show as */*
  Parameters: {"id"=>"6"}
  Post Load (0.5ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", "6"]]
Completed 500 Internal Server Error in 113ms

ActiveRecord::ConfigurationError (Association named 'customers' was not found on Post; perhaps you misspelled it?):
  app/controllers/posts_controller.rb:16:in `show'

해결법

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

    1.이것은 일반적인 오타 오류입니다 :

    이것은 일반적인 오타 오류입니다 :

    @post = Post.joins(:customers).select("customers.*,posts.*").find params[:id]
    # should be:
    @post = Post.joins(:customer).select("customers.*,posts.*").find params[:id]
                              #^^ no plural
    

    이 (사용 단수)와 같은 관계를 정의하기 때문에 :

    # Post model
    belongs_to :customer
    

    어떤 물건을 알고 :

    예를 들면 :

    # Consider these relations:
    User has_many :posts
    Post belongs_to :user
    
    # Usage of joins/includes & where:
    User.includes(:posts).where(posts: { name: 'BlogPost #1' })
                      #^            ^
    Post.joins(:user).where(users: { name: 'Little Boby Table' })
                  #^^           ^
    

    유제:

  2. from https://stackoverflow.com/questions/19231629/association-named-not-found-perhaps-misspelled-issue-in-rails-association by cc-by-sa and MIT license