如何在视图中对与 searchlogic 关联的对象进行排序?
stackoverflow 的伟大知识大师您好, 小编码员学徒 tabaluga 再次需要帮助
目标:使用户名在视图中可排序。困难在于,我正在控制器中查询配置文件( Profile.username 不存在,但 Profile.user.username 存在)。我该如何做到这一点?到目前为止,我的代码
模型代码
Class User < Activerecord::Base
attr_accessible :username
has_one :profile
end
Class Profile < Activerecord::Base
belongs_to :user
end
控制器代码
@search = Profile.search(params[:search])
视图代码
<%= order @search, :by => :user_username %>
好吧,视图代码不起作用(显然)我如何传递视图代码中的关联对象并将其转换为符号?
预先感谢:)
ps 编辑我刚刚发现,这段代码确实有效,抱歉打扰:)
Hello again great knowledge masters of stackoverflow,
once again the small coder apprentice tabaluga is in need of help
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). 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 :)
p.s. EDIT I just figured out, that this code actually works, sorry for bothering :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试在
Profile
模型中创建一个named_scope
,该模型在输入搜索之前按用户对配置文件进行排序。比如:然后
You could try creating a
named_scope
in yourProfile
model which sorts the profiles by user before feeding into your search. Something like:Then