Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

ruby on rails - ids of relation Objects are incremented each time they are saved in other Object and there isn't id saved in one of relation object

I have 3 models: Group, Student and Tutor
Theres one to one relation between Group and Tutor and one to many between Group and Students
When I save Group with Student_ids and Tutor_id, they are incrementing, changing.
For example I save Group with Student_id = 4, and it becomes 5.
Also when I look for student.group it doesn't save, but group.students works correctly. Also can you tell me, why I have to use "tutor" but not "tutor_id" in collection in form? There's my code

Group Model

class Group < ApplicationRecord
  self.primary_key = "group_id"
  self.table_name = "groups"
  has_one :tutor, foreign_key: "tutor_id"
  has_many :tests, foreign_key: "test_id"
  has_many :students, foreign_key: "student_id"
  has_many :lessons, foreign_key: "lesson_id"
end

Student Model

class Student < ApplicationRecord
  self.primary_key = "student_id"
  has_many :parent_students
  has_many :parents, through: :parent_students
  belongs_to :user, foreign_key: "user_id"
  has_many :attendances
  belongs_to :group, optional: true, foreign_key: "group_id"
  has_many :reprimands
  has_many :proposed_grades
  has_many :grades
  has_many :final_grades

  def id_full_name
    "#{id} #{user.name} #{user.surname}"
  end


end

Tutor Model

    class Tutor < ApplicationRecord
  self.primary_key = "tutor_id"
  belongs_to :group, foreign_key: "group_id", optional: true
  belongs_to :user

  def id_full_name
    "#{id} #{user.name} #{user.surname}"
  end

end

Group Controller

    class GroupsController < ApplicationController

  def new 
    @group = Group.new()
  end

  def create 

    @group = Group.new(group_params)
    @group.tutor = Tutor.find(params[:group][:tutor])
    byebug
    @group.save 
    
  end

  def group_params
    params.require(:group).permit(:name, student_ids: [])
  end

end

new group form

 <h1 class="text-center mt-4">Create new Class</h1>
  <div class="container mt-4">
    <%= form_for @group do |f| %>

      <form>
        <div class="form-group row">
          <%= f.label :name, class: "col-2" %>
          <%= f.text_field :name, class: "col-5" %>
        </div>
        <div class="form-group row">
          <%= f.label :tutor, "Tutor", class: "col-2 mt-4" %>
          <%= f.collection_select(:tutor, Tutor.all, :id, :id_full_name,
          {prompt: "Make your selection from the list below"},{size: 4, class: "custom-select shadow rounded col-5" })%>
        </div>
        <div class="form-group row">
          <%= f.label :students_id, "Students", class: "col-2 mt-4" %>
          <%= f.collection_select(:student_ids, Student.all, :id, :id_full_name,
          {prompt: "Make your selections from the list below"},{multiple: true, size: 8, class: "custom-select shadow rounded col-5" })%>
        </div>
   

      <button type="submit" class="btn btn-primary">Create Class</button>
      </form>
    <% end %>
  </div>
</div>
question from:https://stackoverflow.com/questions/65875446/ids-of-relation-objects-are-incremented-each-time-they-are-saved-in-other-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...