2

I'm using rails3 edge and mongoid 2beta6 with ruby 1.9.2-head.

How can I manually change the table name, just like set_table_name for ActiveRecord? For example my model Signup should use the table "users" for storage, not "signups".

Another question is how to implement the bevahior of attr_accessible AR provides?

Thanks, Corin

shingara
  • 46,608
  • 11
  • 99
  • 105
gucki
  • 4,582
  • 7
  • 44
  • 56

3 Answers3

7

Pretty simple :)

Change:

class Pictures
  self.collection_name = 'photos'
end

To:

class Pictures
   store_in collection: 'photos'
end

Source: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-documents/#storage

punnie
  • 2,404
  • 4
  • 17
  • 32
Thomas R. Koll
  • 3,131
  • 1
  • 19
  • 26
  • 1
    Mongoid now also provides `store_in`, as in: `store_in :photos` which accepts an options hash. http://mongoid.org/docs/documents.html – Dan Healy Oct 16 '11 at 09:34
  • 1
    Link above has changed to: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-documents/#storage – mltsy Oct 23 '18 at 21:29
1

With mongoid, as far as I know attr_accessible is ignored. Only attributes that you declare with field will be persisted, however if they are passed as an attribute, you can use attr_accessor to make sure those will not be persist (typically :password as an example).

Alex

Dan Healy
  • 747
  • 7
  • 13
Alex
  • 4,367
  • 5
  • 29
  • 45
0

The link mentioned by Dan Healy was changed to http://mongoid.org/en/mongoid/docs/documents.html.

Community
  • 1
  • 1
Fernando Kosh
  • 3,426
  • 1
  • 34
  • 31