我是 Rails 的新手。我正在尝试在遗留数据库上实现类似 Ryan Bates 的可排序表列代码(Railscast #228)。我的问题与“按关联模型中的列对 Rails 数据库表进行排序”非常相似,但我似乎无法根据那里的答案解决我的问题。
我希望能够按表(Ent 类)中的索引视图对我的项目列表进行排序udtid
,entityudfstorage
即按project.ent.udtid
. 我还有一个额外的考虑,因为每个项目都匹配许多 ent 行,所以我需要将范围匹配到 where ent.rowindex != 0
。
模型:
class Project < ActiveRecord::Base
has_many :ent, :foreign_key => "attachtoid"
has_many :samples, :foreign_key => "projectid"
class Ent < ActiveRecord::Base
set_table_name("entityudfstorage")
belongs_to :project, :foreign_key => "attachtoid"
scope :rowindex, where('entityudfstorage.rowindex != ? ', "0")
项目索引视图:
<tr>
<th><%= sortable "name", "Name" %></th>
<th><%= sortable "projecttype", "Project Type" %> </th>
</tr>
<tr>
<td><%= project.name %></td>
<td><%= project.ent.rowindex.first.udtid %></td>
</tr>
项目负责人
def list
@projects = Project.order(sort_column + " " + sort_direction)
end
我一直在试图弄清楚我可以在 projecttype 的“sort_column”中放入什么,这将使它按关联的字段 project.ent.rowindex.first.udtid 进行排序(与“name”在控制器中的工作方式相同)按 project.name 排序)。
我尝试在以下项目中设置范围
scope :by_udtids, Project.joins("left join ent on projects.projectid = ent.attachtoid").where('ent.rowindex != ?', 0).order("ent.udtid DESC")
然后在项目控制器中尝试了这个。
if sort_column == "projecttype"
@projects = Project.by_udtids
else
@projects = Project.order(sort_column + " " + sort_direction)
结果是项目索引页面在列中显示了正确的数据,但是当我单击“项目类型”链接标题时,它没有排序(然而,如果我单击“名称”链接标题,它确实排序。我在服务器终端中看到的日志对于两次点击都是相同的,并且查询似乎是正确的..
Started GET "/projects?direction=asc&sort=projecttype" for 128.208.10.200 at 2013-08-29 07:47:52 -0700
Processing by ProjectsController#index as HTML
Parameters: {"direction"=>"asc", "sort"=>"projecttype"}
Project Load (1.5ms) SELECT "project".* FROM "project" ORDER BY name asc
Ent Load (0.4ms) SELECT "entityudfstorage".* FROM "entityudfstorage" WHERE "entityudfstorage"."attachtoid" = 602 AND (entityudfstorage.rowindex != '0' ) LIMIT 1
CACHE (0.0ms) SELECT "entityudfstorage".* FROM "entityudfstorage" WHERE "entityudfstorage"."attachtoid" = 602 AND (entityudfstorage.rowindex != '0' ) LIMIT 1
(0.3ms) SELECT COUNT(*) FROM "sample" WHERE "sample"."projectid" = 602
Ent Load (0.3ms) SELECT "entityudfstorage".* FROM "entityudfstorage" WHERE "entityudfstorage"."attachtoid" = 603 AND (entityudfstorage.rowindex != '0' ) LIMIT 1
CACHE (0.0ms) SELECT "entityudfstorage".* FROM "entityudfstorage" WHERE "entityudfstorage"."attachtoid" = 603 AND (entityudfstorage.rowindex != '0' ) LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "sample" WHERE "sample"."projectid" = 603
Rendered projects/list.html.erb within layouts/admin (478.7ms)
Completed 200 OK in 487ms (Views: 398.7ms | ActiveRecord: 87.9ms)
[2013-08-29 07:55:27] WARN Could not determine content-length of response body. Set content- length of the response or set Response#chunked = true
Started GET "/assets/jquery.js?body=1" for 128.208.10.200 at 2013-08-29 07:55:28 -0700
Served asset /jquery.js - 304 Not Modified (0ms)
[2013-08-29 07:55:28] WARN Could not determine content-length of response body. Set content- length of the response or set Response#chunked = true
非常感谢任何见解!