如何处理每个迭代器? [英] How to handle each iterator?

查看:30
本文介绍了如何处理每个迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图页面上的一个表中打印从数据库中两个不同表中获取的值.我不知道如何处理每个迭代器两个,因为它行为异常,即多次打印一个值.我很困惑.请帮忙.

I want to print the values taken from two different tables in the database in a table on the view page. I am not getting how to handle two each iterators as it is behaving abnormally i.e Printing a value several times. I am very much confused. Please help.

这是我的代码

在控制器中:

class ListController < ApplicationController

  def all
    @books = Book.all   
    @susers = SUser.all    
  end
end

在我的视图页面

<tbody>                     
  <% @books.each do |b| %>
    <% if b.branch == "I.T" %>                                      
      <tr>
        <td><%= b.id %></td>
        <td><%= b.book_name %></td>
        <td><%= b.year %></td>
        <td><%= b.user_id %></td>    

        <% @susers.each do |s| %>                   
          <% if s.user_id == b.user_id %>
            <td><%= s.address %></td>
          <% else %>
            <td>Error..!!</td>
          <% end %>
        <% end %>                       
      </tr> 
    <% else %>
      <% puts "No any book of this branch" %>   
    <% end %>                                       
  <% end %>                                 
</tbody>

输出是这样的

第一个 if 语句的 else 部分一次又一次地重复它自己.我不知道为什么会这样?

The else part of the first if statement is repeating it self again and again. I dont know why it is happening?

这个项目有 3 个模型.1. 用户 - 由设计制造2. 预订3. 用户

There are 3 models in this project. 1. User - Made by devise 2. Book 3. SUser

一件重要的事情:-实际上我制作了 SUser 模型,因为我想存储用户的个人详细信息,例如姓名、地址、电话号码.我不想接触设计模型(用户),所以我制作了另一个模型 SUser,它与设计模型(用户)有一对一的关系.

One important thing: - Actually i made SUser model because i want to store user's personal details such as name, address, phone no. I dont want to touch devise model (User) so i made another model SUser which has one to one relation with devise model(User).

用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  has_and_belongs_to_many :books
  has_one :s_user
  devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body

end

图书模型:

class Book < ActiveRecord::Base
  # attr_accessible :title, :body
  has_and_belongs_to_many :users
  belongs_to :s_user, :class_name => "SUser"
  attr_accessible :id, :user_id, :book_name, :edition, :author, :branch, :publisher,      :year, :details 
end

S 用户模型:

class SUser < ActiveRecord::Base
  # attr_accessible :title, :body
  has_one :user
  has_many :books
  attr_accessible :user_id, :fullname, :email, :address, :details 
end

迁移文件:

class CreateBooks < ActiveRecord::Migration
  def change
    create_table :books do |t|
    t.integer "user_id", :limit =>5
    t.string "book_name", :limit => 50
    t.integer "edition", :limit => 5
    t.string "author", :limit => 30
    t.string "branch", :limit => 30
    t.string "publisher", :limit => 50
    t.integer "year", :limit => 10     
    t.text "details"
    t.timestamps
  end
  add_index :books, "user_id"
 end
end

SUser 迁移文件

class CreateSUsers < ActiveRecord::Migration
 def change
  create_table :s_users do |t|
    t.integer "user_id", :limit => 5
    t.string "fullname", :limit => 25            
    t.string "email", :default => "", :null => false
    t.string "hashed_password", :limit => 40
    t.string "salt", :limit => 40
    t.string "address",:limit => 25
    t.text "details"
    t.timestamps
  end
  add_index :s_users, "user_id"
 end
end

我在用户和书之间建立了多对多的关系,因为一个用户有很多本书,而一本书可以提供给很多用户.所以我为多对多关联做了一个简单的连接表

I made many to many relationship between user and book since one user have many books and one book can be available to many users. So i made a simple join table for many to many association

class CreateBooksUsersJoin < ActiveRecord::Migration
 def up
   create_table :books_users, :id => false do |t|
     t.integer "book_id"
     t.integer "user_id"
   end
  add_index :books_users, ["book_id", "user_id"]
 end

 def down
  drop_table :book_users
 end
end

大声笑.. 我已经把我的全部代码都贴在这里了.实际上,我是 Rails 新手.如果您发现此代码的任何其他缺陷,请指导我.谢谢

Lol.. I have pasted my whole code over here. Actually i am new to rails.. Please guide me if you find any other flaw to this code. Thanks

推荐答案

您可以定义模型之间的关系,我认为 one to many 关系类型适合您的情况:

You can define relations between your models, I think one to many relation type is suitable for your situation:

class Book < ActiveRecord::Base
  belongs_to :suser, :class_name => "SUser"
end

class SUser < ActiveRecord::Base
  has_many :books
end

然后在你的控制器中你可以这样写:

Then in your controller you can write like this:

class ListController < ApplicationController
  def all
   @books = Book.includes(:suser).all
  end
end

最后您的视图将如下所示:

And finally your view will look like:

<tbody>                     
<% @books.each do |b| %>
  <% if b.branch == "I.T"%>                                       
  <tr>
    <td><%= b.id%></td>
    <td><%= b.book_name%></td>
    <td><%= b.year%></td>
    <td><%= b.user_id%></td>    
    <td><%= b.suser.try(:address) %></td>
  </tr>   
  <%else%>
    <% puts "No any book of this branch"%>  
  <%end%>                                     
<%end%>                                 
</tbody>

PS:重复 else 块是正常的,因为如果 book.suser_id == suser_id(但书和用户之间是一对多的关系,所以书只属于一个用户,如果你有多对多的关系,则属于少数)

P.S.: it's normal that you have repeating of else block because you check for each user if book.suser_id == suser_id (but there is a one to many relation between books and susers, so book belongs to only one user, to few in case you have many to many relation)

这篇关于如何处理每个迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆