gpt4 book ai didi

ruby-on-rails - 目录 : Listing of subrecords

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:05 32 4
gpt4 key购买 nike

架构:

  • 人(身份证、姓名、出生年份、性别)

  • 宠物(id, person_id, name, leg_count)

  • 植物(id、person_id、种类、数量)

我想做一个关于这些按人分组的只读报告。人员列表已完成(没有相关记录)。我想每个人都有“子表”。像这样的东西:

Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
| 1 | Joe | 1980 | M |
+----+------+-----------+--------+
| Pets |
| +----+------+-----------+ |
| | id | name | Leg count | |
| +----+------+-----------+ |
| | 1 | Rex | 4 | |
| +----+------+-----------+ |
| | 2 | Ka | 0 | |
| +----+------+-----------+ |
| Plants |
| +----+------------+-----+ |
| | id | kind | qty | |
| +----+------------+-----+ |
| | 1 | lemon tree | 2 | |
| +----+------------+-----+ |
+----+------+-----------+--------+
| 2 | Jane | 1982 | F |
+----+------+-----------+--------+
| Pets |
| +----+------+-----------+ |
| | id | name | Leg count | |
| +----+------+-----------+ |
| | 3 | Sue | 6 | |
| +----+------+-----------+ |
| Plants |
| +----+------------+-----+ |
| | id | kind | qty | |
| +----+------------+-----+ |
| | 2 | Oak tree | 1 | |
| +----+------------+-----+ |
+----+------+-----------+--------+

能否请你帮我一些提示,在哪里以及如何 Hook 到框架? (JRuby (1.5.0), Ruby on Rails (2.3.4), ActiveRecord (2.3.4) )

做了什么

Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
| 1 | Joe | 1980 | M |
+----+------+-----------+--------+
| 2 | Jane | 1982 | F |
+----+------+-----------+--------+

使用:

class PersonsReportController < PersonsController
layout 'simpreport'
active_scaffold :person do |config|
c.columns = [:name, :birthyear, :gender, :pets, :plants ]
c.label = "Report of people and other living creatures"
[:pets, :plants].each do |col|
columns[col].clear_link
end
c.list.columns.exclude :pets, :plants
c.actions.exclude :show
end
# ...
end

我还稍微定制了 _list_header.rhtml_list_column_headings.rhtml_list_actions.rhtml 以消除所有交互性(比如订单等等)。

最佳答案

我不明白您在 Controller 中编写的代码,也不明白为什么它会继承自您的 PersonsController。也许您可以详细解释一下您要在那里完成的工作?

话虽这么说,我会这样解决你的问题:

模型:

人.rb:

class Person < ActiveRecord::Base
has_many :pets
has_many :plants

def has_pets?
self.pets.size > 0
end

def has_plants?
self.plants.size > 0
end

def has_pets_or_plants?
self.has_pets? || self.has_plants?
end
end

宠物.rb:

class Pet < ActiveRecord::Base
belongs_to :person
end

植物.rb:

class Plant < ActiveRecord::Base
belongs_to :person
end

Controller :

reports_controller.rb:

class ReportsController < ApplicationController
def index
@persons = Person.find(:all)
end
end

查看:

报告/index.html.erb:

Persons
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>birthyear</td>
<td>gender</td>
</tr>
<% @persons.each do |person| -%>
<tr>
<td><%= person.id %></td>
<td><%= person.name %></td>
<td><%= person.birthyear %></td>
<td><%= person.gender %></td>
</tr>
<% if person.has_pets_or_plants? -%>
<tr>
<td colspan="4">
<% if person.has_pets? -%>
Pets
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>leg count</td>
</tr>
<% person.pets.each do |pet| -%>
<tr>
<td><%= pet.id %></td>
<td><%= pet.name %></td>
<td><%= pet.leg_count %></td>
</tr>
<% end -%>
</table>
<% end -%>
<% if person.has_plants? -%>
Plants
<table border="1">
<tr>
<td>id</td>
<td>kind</td>
<td>qty</td>
</tr>
<% person.plants.each do |plant| -%>
<tr>
<td><%= plant.id %></td>
<td><%= plant.kind %></td>
<td><%= plant.qty %></td>
</tr>
<% end -%>
</table>
<% end -%>
</td>
</tr>
<% end -%>
<% end -%>
</table>

关于ruby-on-rails - 目录 : Listing of subrecords,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3651008/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com