#219
Jun 21, 2010

Active Model

In Rails 3 the non-database functionality of Active Record is extracted out into Active Model. This allows you to cleanly add validations and other features to tableless models.
Tags: models
Download (11.7 MB, 6:47)
alternative download for iPod & Apple TV (10.9 MB, 6:47)

Resources

# models/message.rb
class Message
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming
  
  attr_accessor :name, :email, :content
  
  validates_presence_of :name
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_length_of :content, :maximum => 500
  
  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end
  
  def persisted?
    false
  end
end

# messages_controller.rb
def create
  @message = Message.new(params[:message])
  if @message.valid?
    # TODO send message here
    flash[:notice] = "Message sent! Thank you for conacting us."
    redirect_to root_url
  else
    render :action => 'new'
  end
end

RSS Feed for Episode Comments 25 comments

1. Ryan Jun 21, 2010 at 00:42

Very cool! I'd like to see more OM's in the NoSQL world to start using this, instead of cooking up their own!


2. Matt Powell Jun 21, 2010 at 01:03

Small caveat: ActiveModel defines before_validate and after_validate callbacks, but these aren't really usable: they're mixed up with the validations themselves, and invoked in the order they were defined. So, for example, if you declare a before_validate callback after a validates_presence_of, the before_validate will be called *after* the validation, which isn't very useful.

There's a change in edge now that moves before_validation and friends from ActiveRecord to ActiveModel to fix this, so expect that to make it to the release candidate.


3. dhc Jun 21, 2010 at 10:06

Is it possible to just inherit from ActiveModel::Base?

If so, is there any good reason not to do this, instead of picking and choosing modules and having to dig through code to figure out which dependencies are missing?


4. daniel Jun 21, 2010 at 18:02

same question that dhc posted.


5. jack Jun 22, 2010 at 00:07

informative stuff


6. Steve D Jun 22, 2010 at 10:44

Would also like response to dhc question. From some angles it looks like Rails 3 has a lot of features that come as a box of d-i-y components which you have to know how to assemble youself.

Surely this wasn't the point of Rails?


7. jonas Jun 23, 2010 at 04:39

I think this is very dangerous...

send("#{name}=", value)


8. Jacob Swanner Jun 23, 2010 at 23:27

instead of including ActiveModel::Conversion module, extending ActiveModel::Naming module, and defining the persisted? method...

Another option would have been to change the form_for call in the view. Just change "form_for @message" to "form_for :message, :url => messages_path"


9. Steve D Jun 24, 2010 at 07:22

Would really like answer to dhc question, even if it makes me look even more stupid than I feel already.


10. Casey Jun 24, 2010 at 13:49

@jonas, why? Rails does this in a similar way. Except all it does is check for protected attributes that shouldn't be mass assigned first and that the object responds to "#{key}=". If you want to add your own ability to have protected attributes, then go for it. I just don't think that's in the scope of this railscast.

http://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L1532


11. Dave Jun 24, 2010 at 14:39

Hey faithful Railscast fans:

I just wanted to point out that several of the spammers have websites that also allow comments.

I'm just sayin'...


12. Mark Jun 25, 2010 at 03:58

Just an idea, totally unrelated to the screencast.
You could make a good use of p2p and give torrent links among with the usual download links.


13. Mark Jun 25, 2010 at 11:22

By the way, it's missing rails 3 tag.


14. Dan Simpson Jun 29, 2010 at 19:33

@dhc (Since people are asking): You could define an "ActiveModel::Base" class and include the functionality you want for your app. The nice thing about the modular architecture is that you can do exactly what you are looking to do, but it's not forced upon you. So just define your own base class and include the modules you want for all of your inheriting classes. Don't get this confused with ActiveRecord, as that a different set of classes and modules, that happen to leverage the ActiveModel classes/modules.


15. Owen Jul 06, 2010 at 15:35

Very nice screencast, Ryan! I had the same problem with ActiveRecord on rails 2 which requires some hacks in order to make the model not db specific. Great work on introducing ActiveModel! You mentioned downloading the rails 3 codes and read it. I just found out a neat tool that makes it easier to read codes from a source control system on your browser, it's called CodeFaces, here is the restful link to read the rails code on GitHub: http://codefaces.org/http://github.com/rails/rails


16. Pete Jul 08, 2010 at 07:54

I made a gem called basic_model, based on this approach:

http://github.com/petebrowne/basic_model
http://rubygems.org/gems/basic_model


17. elvankent halı yıkama Jul 22, 2010 at 00:15

Thank you for sharing the article .


18. flowers Jul 22, 2010 at 04:51

Really like this website, this really helps and very useful.


21. wholesale jeans Jul 31, 2010 at 01:25

Just an idea, totally unrelated to the screencast.
You could make a good use of p2p and give torrent links among with the usual download links.


21. aza Aug 02, 2010 at 09:00

Okey


21. dhwstgrghr Aug 05, 2010 at 08:53

jkjokopkp


22. http://www.time4anewpair.com Aug 05, 2010 at 19:30

as that a different set of classes and modules, that happen to leverage the ActiveModel classes/modules.


25. cheap coogi Aug 09, 2010 at 10:19

Here we have popular Polo T-shirt.


26. UGG Boots on sale Aug 10, 2010 at 18:38

Gooooooooooooooooooood luck ~~!!


27. free directory list Aug 11, 2010 at 22:43

Ryan, could you show the view that is calling this template?


28. p90 workout Aug 12, 2010 at 09:13

This is one of the most incredible blogs Ive read in a very long time. The amount of information in here is stunning, like you practically wrote the book on the subject. Your blog is great for anyone who wants to understand this subject more. Great stuff; please keep it up!


29. air jordan retro 13 for sale Aug 14, 2010 at 01:12

Really like this website, this really helps and very useful. like you practically wrote the book on the subject. Your blog is great for anyone who wants to understand this subject more.


30. Rip Blu-ray for Mac Aug 18, 2010 at 01:27

Thanks,it's so good.
suport!


31. supra tk society Aug 18, 2010 at 18:45

good job,good article


32. Seslipanel Aug 19, 2010 at 08:54

Seslichat Seslisohbet Sistemleri


33. Sesli panel Aug 19, 2010 at 08:57

Seslichat Seslisohbet tescilpanel.com tescilpanel Sesli sistemler


34. Seslichat Aug 19, 2010 at 08:57

Seslichat Seslisohbet tescilpanel.com tescilpanel Sesli sistemler


35. Wholesale hats Aug 20, 2010 at 19:58

Good post. I am also going to write a blog post about this...I enjoyed reading your post and I like your take on the issue. Thanks.


36. converse all star Aug 20, 2010 at 20:35

love converse all star,love yourself.Warm welcome.


37. converse all star Aug 20, 2010 at 20:36

love converse all star,love yourself.Warm welcome.


38. converse all star Aug 20, 2010 at 20:53

love converse all star,love yourself.High quality low price.It's fit for you.


39. medyum Aug 22, 2010 at 09:20

The information you provided was very useful. Because of your help, thank you.


40. medyum Aug 22, 2010 at 09:22

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.


41. Indonesia Furniture Handicraft Wholesale Marketplace Aug 23, 2010 at 19:21

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article


42. tile floor vacuum Aug 24, 2010 at 10:26

The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!


43. sesli sohbet panelleri Aug 24, 2010 at 16:05

love converse all star,love yourself.Warm welcome.


44. cheap clothes Aug 24, 2010 at 19:08

David Heinemeier Hansson..thanks


45. PDF to Images Converter Aug 24, 2010 at 22:53

Some times, to a certain need, we have to convert PDF to image for enjoyment.


46. louis vuitton shoes Aug 26, 2010 at 20:55

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


47. mbt shoes sale Aug 28, 2010 at 09:49

you provided was very useful. Because of your help, thank you.


48. cheap mbt shoes Aug 28, 2010 at 10:10

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.


49. cheap ugg boots sale Aug 28, 2010 at 10:20

Great work on introducing ActiveModel! You mentioned downloading the rails 3 codes and read it. I just found out a neat tool that makes it easier to read codes from a source control system on your browser, it's called CodeFaces, here is the restful link to read the rails code on GitHub: http://codefaces.org/http://github.com/rails/rails


50. rap Aug 29, 2010 at 08:47

Thanks for such a great post and the review


51. herve leger dress Aug 30, 2010 at 20:07

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it.


52. winter boots Aug 30, 2010 at 20:08

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it.


53. snow boots Aug 30, 2010 at 20:12

There's a change in edge now that moves before_validation and friends from ActiveRecord to ActiveModel to fix this, so expect that to make it to the release candidate.


54. louis vuitton sunglasses Sep 01, 2010 at 21:18

Good article! Thank you so much for sharing this post.Your views truly open my mind.


55. blu ray ripper Sep 02, 2010 at 00:17

This is really a nice guide for Newbies like me. Thank you.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source