Mongoid - Array management ? insert unique value, remove value if exists ?

786 views
Skip to first unread message

Alexandre Ponsin

unread,
Jan 17, 2011, 1:12:04 AM1/17/11
to Mongoid
Hi,

I am trying to do something rather simple I believe:

1) insert a value in an array field only if that value is not already
present

2) remove a value if it exists in the array

I have just no idea how to do any of this things... for the moment I
am just inserting my value without checking if it exists already:
myArray << obj.id

Thanks,

Alex

ps: using Rails 3.0.3, mongo 1.1.5 and mongoid 2.0.0.rc5

Chris Hanks

unread,
Jan 17, 2011, 2:49:21 AM1/17/11
to Mongoid
Arrays in Mongoid documents are simple Ruby arrays. See the docs for
the Array class: http://www.ruby-doc.org/core/classes/Array.html

So, for insertion you can simply do:

array << object unless array.include?(object)

And for removal:

array.delete(object)



On Jan 16, 10:12 pm, Alexandre Ponsin <alexandre.pon...@gmail.com>
wrote:

Alexandre Ponsin

unread,
Jan 17, 2011, 7:23:39 AM1/17/11
to Mongoid
Yes of course... it makes so much sense !

Thanks for your help. I think I have one final grip, is once you
modified your array etc. you have to save it so your changes persists
to the db right ?

So let's suppose we are in a function defined in your model:

def follow!(obj)
send :following=, [] if (following.nil?)
following << obj.id unless following.include?(obj.id)

self.save

end


The problem is by doing this I only modified my "following" array, the
validation does not go through.
So instead I do save that way: save :validation => false

But even this does not work, I get my traditional your password is too
short blabla because of this:
validates_length_of :password, :minimum => 6, :maximum => 40

How can I get around that ?

Thanks,

Alex

Durran Jordan

unread,
Jan 17, 2011, 11:08:32 AM1/17/11
to mon...@googlegroups.com
use :validate => false, not :validation => false...

I'll get checks in on the association options as well so it's easier to debug these issues. :)

2011/1/17 Alexandre Ponsin <alexandr...@gmail.com>

Alexandre Ponsin

unread,
Jan 17, 2011, 11:36:07 AM1/17/11
to Mongoid
Hi Durran,

Thanks for replying :) Indeed it works... argh I can't believe I did
not see that :(
Yes, some checks would definitely help !

Thanks,

Alex

On Jan 18, 12:08 am, Durran Jordan <dur...@gmail.com> wrote:
> use :validate => false, not :validation => false...
>
> I'll get checks in on the association options as well so it's easier to
> debug these issues. :)
>
> 2011/1/17 Alexandre Ponsin <alexandre.pon...@gmail.com>

Ches Martin

unread,
Jan 17, 2011, 7:42:40 PM1/17/11
to mon...@googlegroups.com
Actually, for the case of validates_length_of :password, what you probably want is to include the :allow_blank => true option for the validator -- how password editing/validation works depends on the authentication library you're using, but the popular ones are designed to ignore a blank password submission on an already-saved model instance. They validate length/presence/etc. for the password only if something other than blank/nil is passed (i.e. you're trying to set a new password). Take a look at how Devise does it, for instance:


If this is basically the way your authentication setup works as well, using :allow_blank => true should get around the problems you're having without resorting to frequent use of :validate => false, which is bound to get you into trouble at some point.
Reply all
Reply to author
Forward
0 new messages