Rails 4 - 如何编写范围以排除一条记录

时间:2016-08-15 02:04:33

标签: ruby-on-rails scope

我正在尝试学习如何在我的Rails 4应用程序中编写范围。

我有称为用户,个人资料,项目和潜在用途的模型。协会是:

用户

   <meta charset="UTF-8">
 <?php
  include '../db.php';
  include '../../lang/lang.ar.php';


           if(empty($_POST['user']) || empty($_POST['ugame']) || empty($_POST['pwd']))
           {
           echo $lang['empty'];
           }else{
            $q=mysqli_query($link,"SELECT * FROM accounts WHERE Username = '".$_POST['ugame']."'");
            $q2=mysqli_query($link,"SELECT * FROM customers WHERE uname = '".$_POST['user']."'");
            if(mysqli_num_rows($q) == 0)
            {
              echo $lang['ugame_!exist'];
            }else if(mysqli_num_rows($q2) != 0)
            {
               echo $lang['exist'];
            }else if(strpos($_POST['user'],';') !== false || strpos($_POST['user'],'-') !== false || strpos($_POST['user'],'#') !== false || strpos($_POST['user'],'@') !== false || strpos($_POST['user'],':') !== false  || strpos($_POST['user'],'*') !== false)
            {
                            echo $lang['not_allowed'];

            }else if(strpos($_POST['user'],' ') !== false){
                    echo $lang['space'];
            }else if(strlen($_POST['pwd']) < 6){
               echo $lang['small_pass'];
            }else if(strlen($_POST['user']) < 6){
               echo $lang['small_user'];
            }else if(strlen($_POST['pwd']) > 14){
               echo $lang['larg_pass'];
            }else if(strlen($_POST['user']) > 32){
               echo $lang['larg_user'];
            }else{

                $date = date("y-m-d");
                $stamp = date('Y-m-d\TH:i:s');

               $done = mysqli_query($link,"INSERT INTO customers  (uname,upass,ugame,date) VALUES ('".$_POST['user']."','".$_POST['pwd']."','".$_POST['ugame']."','".$date."')");
               mysqli_query($link,"INSERT INTO notification  (text,icon,date) VALUES ('New Account registred [ ".$_POST['user']." ]','icon-user','".$stamp."')");
               echo $lang['register_done'];

            }
           }
       ?>

资料

has_one :profile
has_many :potential_uses

项目

belongs_to :user
has_many :projects

潜在用途

belongs_to :profile
has_many :potential_uses

我的潜在用途表中有属性:

belongs_to :project
belongs_to :user

我的潜在用途模型范围包括:

:comment
:internal_comments_permitted
:external_comments_permitted

然后我有一个查找所有第三方评论的类方法:

scope :internal_comments_permitted, ->  where(:internal_comments_permitted => 'true')
    scope :external_comments_permitted, ->  where(:external_comments_permitted => 'true')

现在,我正在尝试编写一个范围,以从第三方评论组中排除具有该项目所属的配置文件的用户所做的任何评论。

我希望有一个视图页面,与项目创建者的评论分开显示第三方评论。

def third_party_comments
  self.internal_comments_permitted.external_comments_permitted
end

我知道上述情况不对,但我无法弄清楚这次尝试有什么问题。

根据Ninigi的调整提出Hasmukh的建议:

scope :third_party_comments, -> where(:comment.user_id != potential_uses.project.profile.user_id)

但这会产生一个错误:

scope :third_party_comments, ->(potential_uses.project.profile.user_id) { where.not(comments: {user_id: potential_uses.project.profile.user_id} ) }

2 个答案:

答案 0 :(得分:2)

试试这个:

scope :third_party_comments, ->where.not(comment.user_id: potential_uses.project.profile.user_id)

参考:Active Record Query Interface

答案 1 :(得分:1)

你几乎就在那里。

scope :third_party_comments, -> where(:comment.user_id != potential_uses.project.profile.user_id)

where接受一个哈希或一系列SQL片段,但你有点有条件。 所以你不能像上面那样完成它,但你可以使用SQL片段,例如try:

scope :third_party_comments, -> { where("comment.user_id <> ?",  potential_uses.project.profile.user_id) }

这使用了Rails的数组语法 - 第一项是一小块纯SQL - 它使用<>来表示&#34;不等于&#34;并使用问号表示“第二个值在这里”#39; (但以安全的消毒方式)

注意:没有经过错误测试,可能需要一些小的跳汰才能使其正常运行...我们可以查看查询界面上的Rails指南,了解更多有关您可以做什么和不能做什么的详细信息:http://guides.rubyonrails.org/active_record_querying.html

(事实上,我强烈建议您阅读所有Rails指南,以了解Rails如何工作)。

我还更新了范围以在lambda周围添加大括号 - 这些日子推荐用于Rails范围。