Skip to content

Commit 028c430

Browse files
Drenmibbatsov
authored andcommitted
Recommend ActiveModel native modules over ActiveAttr gem
1 parent e865060 commit 028c430

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

README.adoc

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,29 +340,41 @@ Introduce non-Active Record model classes freely.
340340

341341
Name the models with meaningful (but short) names without abbreviations.
342342

343-
=== ActiveAttr Gem [[activeattr-gem]]
343+
=== Non-ActiveRecord Models [[non-activerecord-models]]
344344

345-
If you need model objects that support Active Record behavior (like validation) without the Active Record database functionality use the https://github.com/cgriego/active_attr[ActiveAttr] gem.
345+
If you need objects that support ActiveRecord-like behavior (like validations) without the database functionality, use `ActiveModel::Model`.
346346

347347
[source,ruby]
348348
----
349349
class Message
350-
include ActiveAttr::Model
350+
include ActiveModel::Model
351351
352-
attribute :name
353-
attribute :email
354-
attribute :content
355-
attribute :priority
356-
357-
attr_accessible :name, :email, :content
352+
attr_accessor :name, :email, :content, :priority
358353
359354
validates :name, presence: true
360355
validates :email, format: { with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i }
361356
validates :content, length: { maximum: 500 }
362357
end
363358
----
364359

365-
For a more complete example refer to the http://railscasts.com/episodes/326-activeattr[RailsCast on the subject].
360+
Starting with Rails 6.1, you can also extend the attributes API from ActiveRecord using `ActiveModel::Attributes`.
361+
362+
[source,ruby]
363+
----
364+
class Message
365+
include ActiveModel::Model
366+
include ActiveModel::Attributes
367+
368+
attribute :name, :string
369+
attribute :email, :string
370+
attribute :content, :string
371+
attribute :priority, :integer
372+
373+
validates :name, presence: true
374+
validates :email, format: { with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i }
375+
validates :content, length: { maximum: 500 }
376+
end
377+
----
366378

367379
=== Model Business Logic [[model-business-logic]]
368380

0 commit comments

Comments
 (0)