Not*_*ist 1 ruby activerecord activescaffold ruby-on-rails jruby
架构:
persons (id, name, birthyear, gender)
pets (id, person_id, name, leg_count)
plants (id, person_id, kind, qty)
我想制作一份关于按人分组的这些事情的只读报告.完成人员列表(没有相关记录).我希望每个人都有"子表".就像是:
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 | |
| +----+------------+-----+ |
+----+------+-----------+--------+
Run Code Online (Sandbox Code Playgroud)
能否帮我提一些建议,以及如何挂钩框架?(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 |
+----+------+-----------+--------+
Run Code Online (Sandbox Code Playgroud)
它使用:
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
Run Code Online (Sandbox Code Playgroud)
而且我也定制_list_header.rhtml,_list_column_headings.rhtml以及_list_actions.rhtml为了杀死所有交互(如排序等)一点点.
我不理解你在控制器中编写的代码,也不理解为什么它继承自你的PersonsController.也许你可以解释一下你想要在那里完成什么?
话虽这么说,我会像这样解决你的问题:
楷模:
person.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
Run Code Online (Sandbox Code Playgroud)
pet.rb:
class Pet < ActiveRecord::Base
belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)
plant.rb:
class Plant < ActiveRecord::Base
belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)
控制器:
reports_controller.rb:
class ReportsController < ApplicationController
def index
@persons = Person.find(:all)
end
end
Run Code Online (Sandbox Code Playgroud)
视图:
报告/ 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
519 次 |
| 最近记录: |