开发者

Interaction between models and controller in rails 3 with STI

I am implementing a web solution with rails 3 where a user has a list of "Actions". They can either be pending or validated and only an admin can validate.

Two typical actions would be:

1/ Reporting an incorrect picture (if the admin validates then the picture will be deleted hence we need its id) 2/ Submitting a new category (if the admin validates then the category will be added)

Each action has a specific number of point that will go toward the user's reputation which, t me, makes it a good candidate for inheritance (shared behavior at the end of the validation, specific fields and method implementation in the children).

I was planning to have a "onValidate" method in the model that would either delete the picture (1/) or add a new category (2/) but it feels weird开发者_如何学Python to put that in the model.

Can you please let me know how to enhance that design and move that in the controller(s)?


If I understand what you are describing correctly, I don't understand why you think having this code in the model is a bad thing. I take it your User has_many Actions, and an Action has a score. When an action is validated, the state of some other object changes (a picture is deleted, a category is added')

class DeletePictureAction < Action
  belongs_to :user
  belongs_to :picture

  def on_validate
     do_subclass_specific_stuff_to(picture)
     add_to_user_score(score)
  end
end

Nothing here seems to be anything to do with routing and so it has no place in the controller ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜