Code Monkey home page Code Monkey logo

Comments (4)

thetron avatar thetron commented on September 27, 2024

I believe the callbacks on embedded documents are not fired when the parent is saved. From the Mongoid docs:

Callbacks are available on any document, whether it is embedded within another document or not. Note that to be efficient, Mongoid only fires the callback of the document that the persistence action was executed on.

mongoid_token uses callbacks to set the token, so in order to have embedded documents with tokens, you'll need to add the cascade_callbacks option to your relation. This is the example from the docs:

class Band
  include Mongoid::Document
  embeds_many :albums, cascade_callbacks: true
  embeds_one :label, cascade_callbacks: true
end

Hope that helps - let me know if you have any further issues.

from mongoid_token.

Igneous avatar Igneous commented on September 27, 2024

That works perfectly, thank you!

I have one follow-up question. When I try to use the monkeypatched find in one of those embedded documents like:

stack     = StackDef.find(params['stackid'])
component = stack.stack_components.find(params['componentid'])

Or even just:

component = StackDef.find(params['stackid']).stack_components.find(params['componentid'])

find seems to revert to only accepting the mongo ObjectID, rather than the oid and the token. Any idea what's going on there? I've taken to using find_by and just directly searching for the token, but it'd be nice if I was just doing something stupid.

from mongoid_token.

thetron avatar thetron commented on September 27, 2024

Sorry for the delay on this one @Igneous - I was able to replicate your issue, and have added a failing test case. I know it's probably too late you help you on whatever you were working on at the time - but I'm going to see about getting this patched ASAP.

from mongoid_token.

thetron avatar thetron commented on September 27, 2024

@Igneous After exploring this for a while, I think it's confirmed my long-held suspicion that monkey-patching find is a pretty horrible way to handle things.

In the next big release of this gem, I plan on exploring adding support for using the token as the _id field, which would negate the need for the monkey patched finders (Here: #8). I'll close this issue for now, as I think the solution is to use the find_by_* query methods, which was your solution.

Thanks heaps again for all your feedback. 🍰

from mongoid_token.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.