How to get specific field of a many-to-many related document?

31 views
Skip to first unread message

mari...@gmail.com

unread,
Apr 29, 2013, 5:51:12 AM4/29/13
to mongo...@googlegroups.com
Hi,

I'm trying to figure out how exactly I could get a specific field of a many-to-many related document.
Here's a link to my SO question: http://stackoverflow.com/q/15787618/1313143

I have a model called ChildTemplate that has this key:

  key :used_parameters, Array
  many :parameters, :in => :used_parameters

I need only the ID field of the associated parameters. I've tried a number of ways:

used_parameters.name
used_parameters[:name]
parameters.name
parameters[:name]
Parameter.find(used_parameters)[:name]

This is done from within a validation function in the model. All of the above fails. What is the proper way?

Jamie Orchard-Hays

unread,
Apr 29, 2013, 11:35:31 AM4/29/13
to mongo...@googlegroups.com
--
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mari...@gmail.com

unread,
Apr 29, 2013, 11:56:49 AM4/29/13
to mongo...@googlegroups.com
Not really, sorry.. Of course I've read the documentation, but in there I can't find what I'm looking for.
You received this message because you are subscribed to a topic in the Google Groups "MongoMapper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongomapper/Ru5M410KEIM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mongomapper...@googlegroups.com.

Jamie Orchard-Hays

unread,
Apr 29, 2013, 2:53:04 PM4/29/13
to mongo...@googlegroups.com
ct = ChildTemplate.find(<id>)

ct.parameters #=> lazy loads the parameters

ct.parameters.build(:your => value, ...) #=> builds new parameter, but doesn't save
ct.parameters.create(same as above) #=> builds, saves new parameter
ct.parameters << Parameter.new(...) #=> appends, saves new parameter

ct.parameters.first.name #=> name attr of the first parameter

You can create finder methods on your association and there are some built-in.

Have a look in the tests for many more examples:

Reply all
Reply to author
Forward
0 new messages