使用Mongoid向数组添加多个值

时间:2016-08-06 15:23:24

标签: ruby-on-rails ruby-on-rails-4 mongoid

我想向数组添加多个值,但只是第一次添加,下次我想添加内容时,调用会跳过***just before filter ***注释的过程。代码如下:

class ContactController < ApplicationController
  def addNew
    @newFriend = params[:username]
    puts "***value of @newFriend***"
    puts @newFriend
    puts "***end****"
    User.find_by(username: current_user.username) do |user|
      puts "*** just before filter ***"
      user.contacts.find_or_create_by(owner: user.id) do |contact|
        puts "*** enters before find the friend user***"
        User.find_by(username: params[:username]) do |friend|
          puts "***enter on friend***"
          puts friend
          contact.push(friendlist: friend.id)
        end
        puts "***contact***"
        puts contact
        puts "***end****"
      end
    end
    redirect_to "/"
  end
end

1 个答案:

答案 0 :(得分:0)

<强>解决!

最后,我通过了elseclass ContactController < ApplicationController def addNew @newFriend = params[:username] puts "***value of @newFriend***" puts @newFriend puts "***end****" User.find_by(username: current_user.username) do |user| puts "*** aqui entra justo antes de filtrar o crear***" puts user.contacts if user.contacts(owner: user.id).exists? puts "***despues de comprobar si existe entra***" user.contacts.find_by(owner: user.id) do |contact| puts "***despues de crear el contacto entra***" User.find_by(username: params[:username]) do |friend| contact.push(friendlist: friend.id) end end else user.contacts.find_or_create_by(owner: user.id) do |contact| puts "*** aqui entra justo antes de encontrar al usuario amigo***" User.find_by(username: params[:username]) do |friend| puts "***entra en friend" puts friend contact.push(friendlist: friend.id) end end end end redirect_to "/" end end 内双方的联系。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@drawable/view_separator"
android:paddingTop="@dimen/margin_default"
android:paddingBottom="@dimen/margin_default"
android:showDividers="end">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/tagTextView"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/usrTextView"
        />
</LinearLayout>
相关问题