Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1281

Something goes wrong updating my mongoid embedded one to many model

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I think I misunderstood something, but I cannot find out what. My Rails app create an embedded one to many model the first time the user signup, then it schedule twice a day it's updates, ... but duplicate each user entry was previously already saved, each time it run.

      The user model is app/models/user.rb :
      <pre>
      class User
      include Mongoid::Document

      field :provider
      field :uid
      field :name

      embeds_many :watchlists

      def self.create_with_omniauth(auth)

      nickname = auth['user_info']['nickname']
      conn = FaradayStack.build 'https://api.github.com'
      resp = conn.get "/users/#

      {nickname}/watched"

      create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["user_info"]["name"]

      resp.body.each do |repo|
      user.watchlists.build( html_url: "#{repo['html_url']}",
      description: "#{repo['description']}",
      created_at: "#{repo['created_at']}",
      pushed_at: "#{repo['pushed_at']}",
      end
      end
      end
      end
      </pre>

      while the embedded child is app/models/watchlist.rb :

      <pre>
      class Watchlist
      include Mongoid::Document

      field :html_url
      field :description
      field :created_at, :type => DateTime
      field :pushed_at, :type => DateTime

      embedded_in :user
      end
      </pre>

      Now, what seems to be broken, is the scheduled code ( running within rufus-scheduler ):

      <pre>
      conn = FaradayStack.build 'https://api.github.com'
      User.all.map do |user|
      nickname = user.nickname
      resp = conn.get "/users/#{nickname}

      /watched"

      resp.body.each do |repo|
      user.watchlists.build( html_url: "#

      {repo['html_url']}

      ",
      description: "#

      {repo['description']}

      ",
      created_at: "#

      {repo['created_at']}

      ",
      pushed_at: "#

      {repo['pushed_at']}

      " )
      user.update_attributes!
      end
      end
      </pre>

      Where did i go wrong with that ?
      Thanks in advance

            Assignee:
            Unassigned Unassigned
            Reporter:
            ghost Deleted user
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: