简体   繁体   English

Rails-在同一模型上具有多个“ has_many through”关联

[英]Rails - Having multiple 'has_many through 'associations on the same model

I am working on a rails app that registers campers for a camp. 我正在开发一个Rails应用,用于注册营地的营员。 First you sign up using your name, email, etc. Once your profile is made, you can register for a camp. 首先,您使用您的姓名,电子邮件等进行注册。完成个人资料创建后,您可以注册一个营地。 If the camp is not already full, then you are immediately enrolled. 如果训练营尚未满员,那么您将立即注册。 If it is full, then you are put on a waitlist. 如果已满,则将您列入候补名单。 Right now I have 4 models: Campers (name, email, etc.), Camps (name, location, etc.), Enrollments, and Waitlists. 现在,我有4个模型:露营者(姓名,电子邮件等),露营地(姓名,位置等),注册和候补名单。 The idea is to have a camper be able to register to many camps, and obviously a camp having many campers enrolled or waitlisted in it. 这个想法是让一个露营者能够注册到许多营地,并且显然是一个有许多露营者登记或等待名单的营地。 Here are my classes: 这是我的课程:

# camper.rb
has_many :enrolled_in, :class_name => 'Camps', through: :enrollments, dependent: :destroy
has_many :waitlisted_in, :class_name => 'Camps', through: :waitlists, dependent: :destroy

# camp.rb
has_many :enrolled_campers, :class_name => 'Camper', through: :enrollments
has_many :waitlisted_campers, :class_name => 'Camper', through: :waitlists

I'm having trouble with accessing these models through the views. 我在通过视图访问这些模型时遇到了麻烦。 Here is what show.html.erb looks like: 这是show.html.erb的样子:

<!-- Listing camps -->
<h2>Camps</h2>
<p>
  <strong>Name:</strong>
  <%= @camper.enrolled_in.name %> <!-- This is where I get the error -->
</p>

<!-- Adding camps -->
<h2>Add a camp:</h2>
<%= form_with(model: [@camper, @camper.enrolled_in.build ]) do |form| %>
<p>
  <%= form.label :name %><br>
  <%= form.text_field :name %>
</p>
<p>
  <%= form.submit %>
</p>
<% end %>

But I'm getting the following error: 但我收到以下错误:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError in Campers#show Campers#show中的ActiveRecord :: HasManyThroughSourceAssociationNotFoundError

Could not find the source association(s) "enrolled_in" or :enrolled_in in model Enrollment. 在模型注册中找不到源关联“ enrolled_in”或:enrolled_in。 Try 'has_many :enrolled_in, :through => :enrollments, :source => '. 尝试'has_many:enrolled_in,:through =>:enrollments,:source =>'。 Is it one of camp or camper? 是营地还是露营者之一?

And I honestly can't tell what's going wrong. 老实说,我不知道出了什么问题。 I'm fairly new to databases and rails, so go easy on me. 我是数据库和Rails的新手,所以轻松一点。

Look to direction of HABTM associations. 查看HABTM关联的方向。

  1. This will make your code cleaner. 这将使您的代码更整洁。
  2. You will have :after_add and :after_destroy actions that will always track model changes (even direct foreign key inserting, unlike in same named has_many callbacks.) 您将具有:after_add和:after_destroy操作,这些操作将始终跟踪模型更改(甚至直接外键插入,这与相同的has_many回调不同)。

So. 所以。

class Camper
  has_and_belongs_to_many :waitlists, after_add: :check_camp_ready_to_start_as_example
  has_and_belongs_to_many :enrolllists
end

class Waitlist
  belongs_to :camp
  has_and_belongs_to_many :campers
end 

class Enrolllist
  belongs_to :camp
  has_and_belongs_to_many :campers
end

class Campl
  has_many :enrolllists
  has_many :waitlists
end

How to create such migrations (need to create join tables), you can read here 如何创建此类迁移(需要创建联接表),您可以在此处阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM