首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 5多对多索引

Rails 5多对多索引
EN

Stack Overflow用户
提问于 2019-01-12 12:33:19
回答 2查看 60关注 0票数 0

我在展示数据的时候眼睛有问题。

我有3个用户模型,赞助商和宠物结果赞助商在用户和宠物之间的表连接是nan,我的问题是在一个小时内显示所有的吉祥物赞助商的视线和成就,但以错误的方式到La time来传递数据。我希望你能告诉我如何修复它。谢谢。

Index.html.erb

代码语言:javascript
复制
<h1>Sponsors#index</h1>

 <table class="table table-bordered table-hover table-striped" id="histories">
     <thead>
       <tr>
        <th>Mascota</th>
        <th>Padrinos</th>
        <th>Apadrinar</th>
      </tr>
    </thead>
    <tbody> 
     <% @pets.each do |pet| %>
         <tr>
             <td><%= pet.name %></td>
             <td>
                 <% @users.each do |user| %>
                     <% @sponsors.each do |sponsor| %>
                         <% if user.id == sponsor.user_id and pet.id == sponsor.pet_id %>
                             <%= user.email%>
                         <% else %>
                             <p>No Tengo Padinos =-( </p>
                         <% end %>
                     <% end %>
                 <% end %>
              </td>
              <td><button>Apadrinar</button></td>
         </tr>
      <% end %>
     </tbody>
 </table>  

我的控制器有我要发送查看的三个模型。

代码语言:javascript
复制
def index
 @sponsors = Sponsor.all
 @users = User.all
 @pets = Pet.all
end

pet.rb

代码语言:javascript
复制
class Pet < ApplicationRecord
 has_many :adoptions
 belongs_to :race, required: false
 has_many :users, through: :sponsors
end

user.rb

代码语言:javascript
复制
class User < ApplicationRecord
 has_many :pets, through: :sponsors
end

sponsor.rb

代码语言:javascript
复制
class Sponsor < ApplicationRecord
  belongs_to :user
  belongs_to :pet
end

现在的输出显示了图像中的附件。enter image description here

我还想知道是否有更好的方法来进行查询,以显示各自的数据。

另一件事是,如果我不能再赞助已经有赞助商的宠物了,我该怎么办?

EN

回答 2

Stack Overflow用户

发布于 2019-01-13 07:17:31

您正在使用直通表加入用户和宠物,但您的关联设置不正确。对于直通模型,您必须对直通表以及has_many::have_many关联进行直通。您的关联应该如下所示:

代码语言:javascript
复制
class Pet < ApplicationRecord
 has_many :adoptions
 belongs_to :race, required: false
 **has_many :sponsors**
 has_many :users, through: :sponsors
end

class User < ApplicationRecord
 **has_many :sponsors**
 has_many :pets, through: :sponsors
end
票数 0
EN

Stack Overflow用户

发布于 2019-01-14 18:18:30

在工作之后,遵循不同的建议,这是我的解决方案。

代码语言:javascript
复制
<% @pets.each do |pet| %>
 <tr>
  <td><a href="<%= pet_path(pet) %>"> <%= pet.name %></a></td>
  <td>
    <% if pet.sponsors.any? %>
     <% pet.sponsors.each do |sponsor| %>
      | <%= sponsor.user_email %> |
     <% end %>
    <% else %>
     <p>No Tengo Padinos =-( </p>
    <% end %>
   </td>
   <td>
    <%= link_to "Apadrinar", {:controller => "sponsors", :action => "new", :mascot => pet.id }%>
   </td>
 </tr>
<% end %>

我也改变了我的模型。

sponsor.rb

代码语言:javascript
复制
class Sponsor < ApplicationRecord
 belongs_to :user
 belongs_to :pet
 has_many :gifts
 delegate :email, to: :user, prefix: true, allow_nil: true
end

user.rb

代码语言:javascript
复制
class User < ApplicationRecord
 has_many :sponsors
 has_many :pets, through: :sponsors
end

pet.rb

代码语言:javascript
复制
class Pet < ApplicationRecord
 has_many :sponsors
 has_many :users, through: :sponsors
end

最后是控制器上的mi索引。

sponsors_controller.rb

代码语言:javascript
复制
def index
 # @sponsors = Sponsor.all
 # @users = User.all
 # @pets = Pet.all
 # @pets_no_sponsors = Pet.where.not(id: Sponsor.select("pet_id")).select("id")
 @pets = Pet.includes(:sponsors)
end

现在,通过委托,我只进行了一个查询,该查询将所有必要的内容发送到我的索引。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54156791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档