Rails has_many : through, undefined method

MrBalloons

레일에서 다 대다 관계에 문제가 있습니다. 세 가지 모달이 있습니다.

  1. 주문
  2. 안건
  3. 항목 별

주문에는 항목 별 항목을 통해 많은 항목이 있으며 그 반대의 경우도 마찬가지입니다.

Item.find (1) .orders 쿼리는 잘 작동하지만 Order.find (1) .items 시도하면 다음을 반환합니다.

NoMethodError: undefined method `items' for #<Order:0x007fcad3bb3258>

내 코드는 다음과 같습니다.

Schema.rb

create_table "itemizeds", force: :cascade do |t|
  t.integer  "item_id",    limit: 4
  t.integer  "order_id",   limit: 4
  t.integer  "quantity",   limit: 4
  t.datetime "created_at",           null: false
  t.datetime "updated_at",           null: false
end

create_table "items", force: :cascade do |t|
  t.string   "title",      limit: 255
  t.datetime "created_at",             null: false
  t.datetime "updated_at",             null: false
end

create_table "orders", force: :cascade do |t|
  t.integer  "customer_id", limit: 4
  t.integer  "store_id",    limit: 4
  t.integer  "order_id",    limit: 4
  t.datetime "created_at",                                    null: false
  t.datetime "updated_at",                                    null: false
  t.decimal  "price",                 precision: 8, scale: 2
  t.decimal  "discount",              precision: 8, scale: 2
end

Order.rb (모델)

class Order < ActiveRecord::Base
  has_many :itemized
  has_many :items, :through => :itemized
end

Item.rb (모델)

class Item < ActiveRecord::Base
  has_many :itemized
  has_many :orders, :through => :itemized
end

Itemized.rb (모델)

class Itemized < ActiveRecord::Base
  belongs_to :item
  belongs_to :order
end

방해가되는지 확실하지 않지만 매장 모델도 있고 매장에는 주문이 많습니다.

귀하의 도움과 시간에 감사드립니다!

iGian

http://guides.rubyonrails.org/association_basics.html 에서 다 대다 연관을 살펴보면

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments
end

그래서 나는 그것이 복수화의 문제라고 생각합니다. :through => :itemizeds대신 시도하십시오 .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

NoMethodError: undefined method `user' for has_many :through Association

분류에서Dev

Rails has_many : through with the where 절

분류에서Dev

has_many / : through rails4

분류에서Dev

Rails 3: has_many through controller action

분류에서Dev

ActiveRecord, Rails 4 : has_many : through with scoped conditions failure

분류에서Dev

Ruby on Rails has_many : through in a polymorphic association

분류에서Dev

rails cancancan cancan has_many through abilities

분류에서Dev

ordering through an has_many relationship with a condition in rails 3 / active records / postgresql

분류에서Dev

ActiveRecord has_many through inverse_of causing rails admin to work?

분류에서Dev

Rails 4 활성 레코드 모델 has_many through associations?

분류에서Dev

has_many : through in Rails로 생성 및 업데이트

분류에서Dev

Rails 4 : : has_many, : through 및 테이블 이름

분류에서Dev

Ruby on Rails 'has_many : through', 데이터 저장

분류에서Dev

Rails 자체 참조 has_many through : is not added the right record

분류에서Dev

Rails 5.1 has_many through-관련 필드

분류에서Dev

has_many 후 has_many : through?

분류에서Dev

Rails는 has_many : through 관계에서 데이터를 생성합니다.

분류에서Dev

has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

분류에서Dev

has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

분류에서Dev

has_many through : in Rails를 사용하여 현재 및 이전 관계 쿼리

분류에서Dev

중첩 된 속성에 대한 양식 도우미 has_many through in rails 4

분류에서Dev

Rails는`has_many : through` 텍스트 필드를 통해 검색합니다.

분류에서Dev

Rails has_many : through "오류-products.category 열이 존재하지 않습니다"

분류에서Dev

has_many, through, class_name 및 where 절이있는 Rails 모델 연결

분류에서Dev

link to relationship name in has_many :through

분류에서Dev

has_many : through 양식

분류에서Dev

Finding Users with has_many :through

분류에서Dev

Make has_many :through not deleteable

분류에서Dev

Join has_many :through attributes

Related 관련 기사

  1. 1

    NoMethodError: undefined method `user' for has_many :through Association

  2. 2

    Rails has_many : through with the where 절

  3. 3

    has_many / : through rails4

  4. 4

    Rails 3: has_many through controller action

  5. 5

    ActiveRecord, Rails 4 : has_many : through with scoped conditions failure

  6. 6

    Ruby on Rails has_many : through in a polymorphic association

  7. 7

    rails cancancan cancan has_many through abilities

  8. 8

    ordering through an has_many relationship with a condition in rails 3 / active records / postgresql

  9. 9

    ActiveRecord has_many through inverse_of causing rails admin to work?

  10. 10

    Rails 4 활성 레코드 모델 has_many through associations?

  11. 11

    has_many : through in Rails로 생성 및 업데이트

  12. 12

    Rails 4 : : has_many, : through 및 테이블 이름

  13. 13

    Ruby on Rails 'has_many : through', 데이터 저장

  14. 14

    Rails 자체 참조 has_many through : is not added the right record

  15. 15

    Rails 5.1 has_many through-관련 필드

  16. 16

    has_many 후 has_many : through?

  17. 17

    Rails는 has_many : through 관계에서 데이터를 생성합니다.

  18. 18

    has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

  19. 19

    has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

  20. 20

    has_many through : in Rails를 사용하여 현재 및 이전 관계 쿼리

  21. 21

    중첩 된 속성에 대한 양식 도우미 has_many through in rails 4

  22. 22

    Rails는`has_many : through` 텍스트 필드를 통해 검색합니다.

  23. 23

    Rails has_many : through "오류-products.category 열이 존재하지 않습니다"

  24. 24

    has_many, through, class_name 및 where 절이있는 Rails 모델 연결

  25. 25

    link to relationship name in has_many :through

  26. 26

    has_many : through 양식

  27. 27

    Finding Users with has_many :through

  28. 28

    Make has_many :through not deleteable

  29. 29

    Join has_many :through attributes

뜨겁다태그

보관