has_many :through Association in Rails

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

ความคิดเห็น • 14

  • @ChandanKumar-h4b
    @ChandanKumar-h4b 6 หลายเดือนก่อน +1

    Superb content buddy! Really appreciate your hard work 🙌

  • @usamaislam1419
    @usamaislam1419 2 ปีที่แล้ว +2

    excellent work brother

  • @codenet1
    @codenet1 2 ปีที่แล้ว +1

    How do one access the students project submission date

  • @MyOpinion_MyReview
    @MyOpinion_MyReview ปีที่แล้ว

    Can you explain build method

  • @geekysunny01
    @geekysunny01 2 ปีที่แล้ว +1

    indian guy

  • @vigneshwarsathyanarayanan
    @vigneshwarsathyanarayanan 2 ปีที่แล้ว +1

    When I try creating a StudentProject
    the transaction gets rolled back
    not sure why
    StudentProject.create(student_id: 10, project_id: 10, submission_date: Date.today + 30.days)
    TRANSACTION (0.1ms) begin transaction
    Student Load (0.1ms) SELECT "students".* FROM "students" WHERE "students"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
    Project Load (0.0ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
    TRANSACTION (0.0ms) rollback transaction

    • @APPSIMPACTAcademy
      @APPSIMPACTAcademy  2 ปีที่แล้ว +1

      Can you please try this way?
      student_project = StudentProject.new(student_id: 10, project_id: 10, submission_date: Date.today + 30.days)
      student_project.valid?
      If this returns false, then please try to see the errors by running:
      student_project.errors.full_messages

    • @APPSIMPACTAcademy
      @APPSIMPACTAcademy  2 ปีที่แล้ว

      let me know if this works for you

    • @vigneshwarsathyanarayanan
      @vigneshwarsathyanarayanan 2 ปีที่แล้ว

      @@APPSIMPACTAcademy i am getting this
      irb(main):022:0> student_project.errors.full_messages
      => ["Project must exist"]

    • @APPSIMPACTAcademy
      @APPSIMPACTAcademy  2 ปีที่แล้ว

      @@vigneshwarsathyanarayanan you need to check if the project model has some instances or not

    • @vigneshwarsathyanarayanan
      @vigneshwarsathyanarayanan 2 ปีที่แล้ว

      @@APPSIMPACTAcademy i followed the lecture and in the project model there are only associations present
      class Project < ApplicationRecord
      has_many :student_projects
      has_many :students, through: :student_projects
      end