name_scope 不显示正确的列
更新
我想做的 是选择特定的列并在它们上使用别名,然后在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你在控制台中调用
User.prog_result
时,你实际上是在调用User.prog_result.inspect
,控制台是通过调用#inspect
来显示结果的在结果对象上。相反,如果您在控制台中调用
User.prog_result.class
,您会看到它是一个ActiveRecord::NamedScope::Scope
。如果您深入研究这些返回的对象,您可能会发现您所期望的内容。
Rails 3.1 中已弃用命名范围和传递
:joins
、:conditions
和:select
,因此您可能不应该使用以下代码开发新代码除非您使用的是 Rails 2.3.x。你会想做这样的事情:When you call
User.prog_result
in the console, you are really callingUser.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 anActiveRecord::NamedScope::Scope
.If you poke inside these objects that are being returned, you'll probably find what you are expecting.
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 对象,并且 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