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

Embedded and belongs_to with the same model problem

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.1.9
    • Affects Version/s: None
    • Component/s: None

      Hi,

      I have Models ( User, Participation, Comment, Report)

      class User

      embeds_many :reports, as: :repotable

      end

      class Report

      belongs_to :reporter, class_name: 'User'

      embedded_in :reportable, polymorphic: true
      end

      class Participation

      embeds_many :reports, as: :reportable
      end

      class Comment

      embeds_many :reports, as: :reportable
      end

      so the problem here is in the relation between Report and User because they are related in two ways 1- report belongs to user and 2 - it may be or may be not embedded in a user model according to the behavior because i can report a user for x and i can report a participation for y and can report a comment for z.

      Note: Participation is embedded inside a another table and this table is related to the user and Comment is embedded inside Participation.

      when i write
      report = User.first.reports.new
      report.reporter = User.last

      i am getting this error

      NoMethodError: undefined method `first' for #<Report:0x8d38cec>
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:392:in `method_missing'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/attributes.rb:153:in `method_missing'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/builders/embedded/many.rb:24:in `build'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:43:in `create_relation'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:26:in `build'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:145:in `block (2 levels) in setter'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/extensions/object/yoda.rb:22:in `do_or_do_not'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/bindings/referenced/in.rb:34:in `block in bind'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/binding.rb:21:in `binding'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/bindings/referenced/in.rb:23:in `bind'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/proxy.rb:20:in `bind_one'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/referenced/in.rb:24:in `block in initialize'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/proxy.rb:35:in `init'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/referenced/in.rb:22:in `initialize'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:44:in `new'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:44:in `create_relation'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:26:in `build'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/mongoid-2.1.5/lib/mongoid/relations/accessors.rb:145:in `block (2 levels) in setter'
      from (irb):3
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
      from /home/.rvm/gems/ruby-1.9.2-p180@rails-3/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
      from script/rails:6:in `require'
      from script/rails:6:in `<main>'ruby-1.9.2-p180 :004 >

      so any help ?? i am trying to find a solution but i can't

      Thanks in advance,
      RR

            Assignee:
            Unassigned Unassigned
            Reporter:
            ranaramez ranaramez
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: