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

发布于 2024-09-08 15:26:48 字数 633 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-09-15 15:26:48

您可以尝试在 Profile 模型中创建一个 named_scope,该模型在输入搜索之前按用户对配置文件进行排序。比如:

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

然后

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

You could try creating a named_scope in your Profile model which sorts the profiles by user before feeding into your search. Something like:

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

Then

@search = Profile.sorted_by_user.search(params[:search])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文