更新
我想要做的是選擇特定列並使用它們的別名,並在named_scope中使用它。我想使用別名,因爲稍後我會基於我創建的SQL嘗試連接多個表時將其用作想法。named_scope不顯示正確的列
編輯: 林對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\">]"
誰能告訴我什麼我做錯了嗎?..
對不起,我正在運行Rails 2.3.8我編輯了我的問題。你對'User.prog_result.class'是正確的,它確實在控制檯中返回了'ActiveRecord :: NamedScope :: Scope',並且在它內部戳了一下。但是,當我試圖得到部門它返回'nil' idk爲什麼,但與我以前的代碼我用'User.find_by_sql()'我有他們的部門.. – jovhenni19