name_scope 不显示正确的列

发布于 2024-12-06 02:35:10 字数 1059 浏览 0 评论 0原文

更新

我想做的 是选择特定的列并在它们上使用别名,然后在named_scope中使用它。我想使用别名,因为稍后当我尝试根据我创建的 SQL 连接许多表时,我将使用它作为一个想法。

编辑: 我在Rails 2.3.8和Ruby 1.8.7中运行,


这是我的named_scope...

named_scope :prog_result,:select => "users.id AS 'user_id', users.username AS 'username', users.lastname AS 'lastname', users.firstname AS 'firstname', departments.name AS 'department', versions.number AS 'version'", 
                         :joins => {:department => :version}

当我尝试在控制台User.prog_result中调用它时,结果是这样...

[#<User username: "respondent1", lastname: "res", firstname: "pon">, #<User username: "2222", lastname: "Numero", firstname: "Dos">]

User. prog_result.find(2) 2是用户“respondent1”的id,User.prog_result.inspect的结果是这个

"[#<User username: \"respondent1\", lastname: \"res\", firstname: \"pon\">, #<User username: \"2222\", lastname: \"Numero\", firstname: \"Dos\">]"

有人能告诉我我是什么吗做错了吗?...

Update

I am trying to do is select specific columns and use aliases on them, and use it in a named_scope. I want to use aliases because later on I'll use this as an idea when I try to join many tables, based on the SQL that I have created.

Edit:
Im running in Rails 2.3.8 and Ruby 1.8.7


Here is my named_scope...

named_scope :prog_result,:select => "users.id AS 'user_id', users.username AS 'username', users.lastname AS 'lastname', users.firstname AS 'firstname', departments.name AS 'department', versions.number AS 'version'", 
                         :joins => {:department => :version}

And when I try to call it in console User.prog_result the result is this...

[#<User username: "respondent1", lastname: "res", firstname: "pon">, #<User username: "2222", lastname: "Numero", firstname: "Dos">]

User.prog_result.find(2) 2 is the id of the user "respondent1", the result of User.prog_result.inspect is this

"[#<User username: \"respondent1\", lastname: \"res\", firstname: \"pon\">, #<User username: \"2222\", lastname: \"Numero\", firstname: \"Dos\">]"

Can someone tell me what am I doing wrong?..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

岁月如刀 2024-12-13 02:35:10

当你在控制台中调用User.prog_result时,你实际上是在调用User.prog_result.inspect,控制台是通过调用#inspect来显示结果的在结果对象上。

相反,如果您在控制台中调用 User.prog_result.class,您会看到它是一个 ActiveRecord::NamedScope::Scope

如果您深入研究这些返回的对象,您可能会发现您所期望的内容。

User.prog_result.first.department => first joined department.name
User.prog_result.last.version   => last joined version.number

Rails 3.1 中已弃用命名范围和传递 :joins:conditions:select,因此您可能不应该使用以下代码开发新代码除非您使用的是 Rails 2.3.x。你会想做这样的事情:

User.joins(:department => :version).
select("users.id AS 'user_id', users.username AS 'username', users.lastname AS 'lastname', users.firstname AS 'firstname', departments.name AS 'department', versions.number AS 'version'")

When you call User.prog_result in the console, you are really calling User.prog_result.inspect The console is displays the result by calling #inspect on the result object.

If instead you called User.prog_result.class in the console, you'd see that it was an ActiveRecord::NamedScope::Scope.

If you poke inside these objects that are being returned, you'll probably find what you are expecting.

User.prog_result.first.department => first joined department.name
User.prog_result.last.version   => last joined version.number

Both named scopes and passing :joins, :conditions and :select are deprecated in Rails 3.1, so you should probably not be developing new code with them unless you are on Rails 2.3.x. You'll want to do something like this:

User.joins(:department => :version).
select("users.id AS 'user_id', users.username AS 'username', users.lastname AS 'lastname', users.firstname AS 'firstname', departments.name AS 'department', versions.number AS 'version'")
柠檬色的秋千 2024-12-13 02:35:10

我不太确定你的问题,但我假设你正在查看一个 User 对象,并且 Rails 返回一个数组。

如果是这种情况,named_scope 将始终返回一个数组(就像您执行 User.find_all() 一样),

即使只有一条记录

希望我正确回答了您的问题..

感谢并问候

Sameera

I'm not quit sure about your question, But I assume you are looking at a User object and rails returns an array.

If thats the case, named_scope will always return an array (just like you do a User.find_all())

even if there is only one record

Hope i got your question right ..

thanks and regards

sameera

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