合并Rails对象,仅保留当前值

时间:2016-11-15 02:04:54

标签: ruby-on-rails object merge

我有一个对象数组,我想合并为一个:

<?php
    $connect      = mysqli_connect("localhost", "root", "", "database");
    global $connect;   

    if(isset($_POST['Submit'])){
        $input       = $_POST['input'];

        $sql = "SELECT * FROM table";
        $get = mysqli_query($connect,$sql);
        if($get && mysqli_num_rows($get) > 0 ){ 
            while($row = mysqli_fetch_assoc($get)){
                $input_db  = $row['input'];
                $output_db = $row['output'];

                if (($input >= $input_db) && ($output <= $output_db))
                {
                    echo "Input and output is in between";
                    exit();
                }
                else
                {
                    echo "Data added ! <br/>";
                    exit();
                }
            }
        mysqli_free_result($get);   
        }
        else
        {
            echo "data is outside range";
        }
    }
?>
<form action="testdate.php" method="post">  
    <table> 
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Input : </td>
            <td><input type ="text" name="input" size="30"></td>
        </tr>
    </table>    

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>              
</form>

这应该成为:

[
  #<User firstname: 'John', middlename: '',   lastname: nil>
  #<User firstname: '',     middlename: '',   lastname: 'Doe'>
  #<User firstname: nil,    middlename: 'W.', lastname: nil>
]

是否有一种简单的方法或者我必须遍历所有对象,查看#<User firstname: 'John', middlename: 'W.', lastname: 'Doe'> 并构建一个新对象?

更新:我目前的代码状态

.attributes

嗯,它有效,但代码看起来不是很干净......

0 个答案:

没有答案
相关问题