FAQ
Is this expected behavior? A new Character document is being initialized
each time I call Account#character even though it has already loaded the
referenced character document and returns the same object each time, as I
expect. Also, Account isn't automatically loading its characters.

class Character
include Mongoid::Document

field :name, type: String

belongs_to :account

after_initialize :init

def initialize(name, account)
super()
self.name = name
self.account = account
end

def init
p "New Character object -- #{self.object_id}"
end

end

class Account
include Mongoid::Document
include Mongoid::Timestamps

field :name, type: String

has_many :characters

after_initialize :init

def initialize(name)
super()
self.name = name
end

def init
#self.characters.inspect
end

def character
p "Account#character called."
self.characters.each do |ch|
return ch
end
nil
end
end

acc = Account.find_by(name: "testaccount")
p "Account loaded."
p acc.character.object_id
p acc.character.object_id

Output:
"Account loaded."
"Account#character called."
"New Character object -- 21818640"
21818640
"Account#character called."
"New Character object -- 21845000"
21818640


Also, if I uncomment the #characters.inspect call in Account#init then I
get the output I was initially expecting:

"New Character object -- 27135180"
"Account loaded."
"Account#character called."
27135180
"Account#character called."
27135180


Like I said, just wondering if this is expected behavior and maybe get an
explanation for why calling #characters.inspect solves my issue.

Thanks for any help,
Matt

--

---
You received this message because you are subscribed to the Google Groups "Mongoid" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoid+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Durran Jordan at Mar 4, 2013 at 12:42 pm
    You are overriding Document#initialize but not passing the correct
    parameters to super, so my expectation is many things will not work as
    expected. Can you not do this and just pass a block? Initializing documents
    needs a hash of attributes, and with 3.x potential mass assignment options.
    By changing this signature this will cause all sorts of pain. For example
    Mongoid internally will be trying to set name to a hash and account to a
    hash for any time it calls a constructor.

    Instead do something like:

    def self.init(name, account)
    Character.new do |character|
    character.name = name
    character.account = account
    end
    end

    2013/2/27 Matthew Gardner <mbgardner21@gmail.com>
    Is this expected behavior? A new Character document is being initialized
    each time I call Account#character even though it has already loaded the
    referenced character document and returns the same object each time, as I
    expect. Also, Account isn't automatically loading its characters.

    class Character
    include Mongoid::Document

    field :name, type: String

    belongs_to :account

    after_initialize :init

    def initialize(name, account)
    super()
    self.name = name
    self.account = account
    end

    def init
    p "New Character object -- #{self.object_id}"
    end

    end

    class Account
    include Mongoid::Document
    include Mongoid::Timestamps

    field :name, type: String

    has_many :characters

    after_initialize :init

    def initialize(name)
    super()
    self.name = name
    end

    def init
    #self.characters.inspect
    end

    def character
    p "Account#character called."
    self.characters.each do |ch|
    return ch
    end
    nil
    end
    end

    acc = Account.find_by(name: "testaccount")
    p "Account loaded."
    p acc.character.object_id
    p acc.character.object_id

    Output:
    "Account loaded."
    "Account#character called."
    "New Character object -- 21818640"
    21818640
    "Account#character called."
    "New Character object -- 21845000"
    21818640


    Also, if I uncomment the #characters.inspect call in Account#init then I
    get the output I was initially expecting:

    "New Character object -- 27135180"
    "Account loaded."
    "Account#character called."
    27135180
    "Account#character called."
    27135180


    Like I said, just wondering if this is expected behavior and maybe get an
    explanation for why calling #characters.inspect solves my issue.

    Thanks for any help,
    Matt

    --

    ---
    You received this message because you are subscribed to the Google Groups
    "Mongoid" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to mongoid+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --

    ---
    You received this message because you are subscribed to the Google Groups "Mongoid" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to mongoid+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupmongoid @
categoriesmongodb, rubyonrails
postedFeb 28, '13 at 9:37a
activeMar 4, '13 at 12:42p
posts2
users2
websitemongoid.org
irc#mongoid

2 users in discussion

Durran Jordan: 1 post Matthew Gardner: 1 post

People

Translate

site design / logo © 2022 Grokbase