如何处理每个迭代器?

时间:2012-10-25 20:44:28

标签: ruby-on-rails-3

我想在视图页面的表格中打印数据库中两个不同表格的值。我没有得到如何处理两个每个迭代器,因为它表现异常,即多次打印一个值。我很困惑。请帮忙。

这是我的代码

在控制器中:

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>

输出显示如下

第一个else语句的if部分会一次又一次地重复它。我不知道为什么会这样?You can see the error part printing four times since the book table has 4 books in the database.

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

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

用户模型:

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

SUser模型:

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

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

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的新手..如果您发现此代码有任何其他缺陷,请指导我。 感谢

2 个答案:

答案 0 :(得分:1)

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

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

class SUser < ActiveRecord::Base
  has_many :books
end

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

class ListController < ApplicationController
  def all
   @books = Book.includes(: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>    
    <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(但是书籍与激情之间存在一对多关系) ,所以本书只属于一个用户,少数以防你有多对多的关系)

答案 1 :(得分:1)

class Book < ActiveRecord::Base
  belongs_to :suser
end

class SUser
  has_many :books
end

class ListController < ApplicationController
  def all
    @books = Book.includes(:susers).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>    
    <td><%= b.suser.address %></td>
  </tr>   
  <%else%>
    <% puts "Branch has no books"%>  
  <%end%>                                     
<%end%>                                 
</tbody>

最后,您需要一个外键用于关系,例如:

script/generate migration add_user_id_to_books

迁移语法可能很棘手,因此打开迁移文件(在db / migrate中)并确保它正在执行类似于     add_column:books,user_id,integer