RoR model field without validators, has*, delegates, etc

Posted by jackr on Stack Overflow See other posts from Stack Overflow or by jackr
Published on 2012-11-26T23:02:43Z Indexed on 2012/11/26 23:03 UTC
Read the original article Hit count: 165

Filed under:
|

How can I declare a field, in the Rails model, when it doesn't have any "has_" relations, or validations, or delegations? I just need to ensure its existence and column width in the schema.

Currently, I have no mention of the field in the "schema section" of the model file, but it's referenced in various methods that use it, and this seems to work. However, depending on my exact creation workflow, the underlying database table may be created as

t.binary "field_name", :limit => 32

or

t.binary "field_name", :limit => 255

This is not a restriction on the value (any binary value is valid, even NULL), only on the table column declaration. As it happens, 32 is enough -- it never receives any larger value, it's only ever written to like this:

self.field_name = SecureRandom.random_bytes(32)

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about model