简体   繁体   English

如何在视图中使用searchlogic对关联对象进行排序?

[英]How do I order associated objects with searchlogic in views?

Hello again great knowledge masters of stackoverflow, once again the small coder apprentice tabaluga is in need of help 再次向您问好Stackoverflow的知识渊博的大师,小型编码员徒弟Tabaluga再次需要帮助

The Goal : make the username sortable in the view. 目标:使用户名在视图中可排序。 The difficulty is, that I am Querying Profiles in the controller ( Profile.username doesn't exist but Profile.user.username does). 困难在于,我正在控制器中查询配置文件(Profile.username不存在,但Profile.user.username确实存在)。 How Do I accomplish that? 我该怎么做? My Code so far 到目前为止,我的代码

model code 型号代码

Class User < Activerecord::Base
   attr_accessible :username
   has_one :profile
 end

 Class Profile < Activerecord::Base
   belongs_to :user
 end

controller code 控制器代码

@search = Profile.search(params[:search])

view code 查看代码

<%= order @search, :by => :user_username %>

okay, the view code doesn't work (obviously) how can I pass the associated object in the view code and convert it to a symbol? 好的,视图代码不起作用(很明显),如何在视图代码中传递关联的对象并将其转换为符号?

Thanks in advance :) 提前致谢 :)

ps EDIT I just figured out, that this code actually works, sorry for bothering :) ps编辑我刚刚想通了,此代码实际上有效,抱歉打扰:)

You could try creating a named_scope in your Profile model which sorts the profiles by user before feeding into your search. 您可以尝试在个人Profile模型中创建一个named_scopenamed_scope在将其输入搜索之前按用户对个人Profile进行排序。 Something like: 就像是:

named_scope :sorted_by_user, { :include => :user, :conditions => ["ORDER BY user.username"] }

Then 然后

@search = Profile.sorted_by_user.search(params[:search])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM