16

I have been following the mongoid railscast and I am facing a multi parameter attributes problem when i add a new field field :published_on, :type => Date

Article.find("4da14b1447640b14eb000002").published_on 
=> nil

but with Article.find('4da14b1447640b14eb000002')

=> published_on: nil, published_on(1i): "2011", published_on(2i): "6", published_on(3i): "10"

How do i get the desire output?

i know there is a temporary solution https://gist.github.com/315227

but the issue of this problem is closed and I have the latest 2.0.1 mongoid gem and yet the problem occur.

wizztjh
  • 6,979
  • 6
  • 58
  • 92

2 Answers2

22

Modify your Article class so it looks like the one below.

class Article
  include Mongoid::Document
  include Mongoid::MultiParameterAttributes

  ...
end
zaiste
  • 547
  • 4
  • 9
  • 1
    it is still the same ruby-1.9.2-p136 :002 > Article.first.published_on => nil – wizztjh Apr 11 '11 at 06:55
  • 2
    Existing articles need to be updated. Have you tried to create a new article and see if it works ? – zaiste Apr 11 '11 at 11:12
  • It also works if you edit/update the record. My thanks as well Zaiste. You'd think this would be flagged in a more prominent way in the Mongoid docs... – Joe Jun 11 '12 at 18:40
5

In case you're using Mongoid 4.0 - the Mongoid::MultiParameterAttributes has been removed there.

If you need this functionality anyway you may take the classes from this blog.

Konstantin Rudy
  • 2,237
  • 25
  • 22