Fernando Perez
2009-May-02 16:00 UTC
[rspec-users] RSpecers point of view about how to create rails association
Hi, Using Ruby on Rails, let''s says I have an Article model and Comment model. An Article has_many :comments, Using Rais idiom, one would do something like that in the comments_controller/create: @article = Article.find(params[:id]) @article.comments.create(params[:comment]) I don''t like too much this approach because it puts too much intelligence into the controller and it''s not easy to test/spec/mock. So I''d like to push this into a model. So would you tell Article model to create a new Comment given params[:comment], or would you tell Comment to create a new Comment given an article_id?>From a BDD point of view, only creating a new comment record is whatcounts, but I''d like to be more careful about Demeter, encapsulation, tell don''t ask, ... -- Posted via http://www.ruby-forum.com/.
Fernando Perez
2009-May-02 20:42 UTC
[rspec-users] RSpecers point of view about how to create rails association
> You *could* write wrapper methods for chains like this and make Demeter > happy, but to do that for every single call...Thank you for your experience. I myself used to wrap these kind of associations but I felt it started to clutter the Model file, so I started to use association callbacks. My heart balances between both ideas. Anyway if attr_accessible is used, then you must write a wrapper, but in other cases? Which is the most effective / maintainable / etc is still to be questioned. Honestly, each time something backfired at me in a very unexpected manner, it was due to the fact that somewhere far far away I had broken Demeter''s law. So I''d like to get this fixed once and for all. -- Posted via http://www.ruby-forum.com/.