Topic: I'm confused by REST!

I'm trying to understand how best to implement REST and keep to REST principles within my application.  I can certainly see how REST is useful for simple parent children relationships and how the idea of resources can ensure your database is useful when accessed via a number of methods html/xml etc.

However I'm trying to figure how it helps simplify more complicated database associations.

For instance, I am coding a rails application that has a "report" resource upon which the relationship between "tasks" and "operatives" are recorded on a daily basis.

i.e.

report <--many to many --> operatives
report <-many to many --> tasks
tasks <-many to many --> operatives

Setting up the models is fine (has_many :through is great) and it works however I'm confused when I come to work with editing these relationships.

The idea is each report can be edited as its own resource and tasks/operatives/assignments edited and created on that page.

However this necesitates creation and destruction of other model records from the report's edit form.  Surely this breaks the REST principle, i'm no longer calling resource CRUD methods but rather using the ReportController's edit method to effectively RPC and manhandle my records.

Now I'm at a stage where I need to create new "task" records based on entries in the report form and handled by the report controller. (and obviously in turn the taskassignments which relates those tasks to operatives).

Ultimately I would like Ajax to handle the addition of new tasks and dynamically update the form surely this would require calling of methods outside of the resources standard CRUD methods again is this against REST or have I got myself confused?

Sorry for the rambling I know I can hack together a solution but I dont want to get into bad habits and make a bad design choice for my application which I would regret later.

Thanks for the help!

Re: I'm confused by REST!

rebo wrote:

report <--many to many --> operatives
report <-many to many --> tasks
tasks <-many to many --> operatives

Is there really a many-to-many relationship between all three of these? If you are using has_many :through between them, wouldn't there be a join model between each one?


rebo wrote:

However this necesitates creation and destruction of other model records from the report's edit form.  Surely this breaks the REST principle, i'm no longer calling resource CRUD methods but rather using the ReportController's edit method to effectively RPC and manhandle my records.

This can sometimes be solved with AJAX which makes calls to the other controllers (for managing the other models). However, if you don't want to go this route, I don't see a problem with the Reports controller managing multiple models in the update form as long as it doesn't get too complex. If you end up having to add more actions to the Reports controller to manage these other models, then I see that becoming a problem.

If you want to keep this form out of the Reports controller so it isn't managing multiple models, that's perfectly fine. You can create another controller (non RESTful) which has this form and handles the action. I would try one of the above suggestions first though. If you need me to expound on one of those, I can do so.


rebo wrote:

Ultimately I would like Ajax to handle the addition of new tasks and dynamically update the form surely this would require calling of methods outside of the resources standard CRUD methods again is this against REST or have I got myself confused?

As mentioned above, the Ajax can call an action in another controller - whichever suits the action. So if the AJAX command is creating a task, it would call the Tasks controller "create" action.

Railscasts - Free Ruby on Rails Screencasts

Re: I'm confused by REST!

Is there really a many-to-many relationship between all three of these? If you are using has_many :through between them, wouldn't there be a join model between each one?

Yes heh, gotta rush out the door to work but very quickly, the report and operatives are associated thru "assignments", the operatives and tasks thru "taskassignments" and the tasks and reports thru "taskonreports".

The idea is that I can have 1 task with associated description that appears on many different report sheets.  The task on reports handles that but also thru an extra attribute on this through model I can record how much work was done that particular night.

For instance, task 1 may be "Paint Walls" and taskonreport may record that task being done on many different nights i.e. many different "reports" but also thru an extra attribute it can record how much wall painting was done!.  When ill get back ill throw up a screenshot of the report screen and break it down you.

Thanks alot for the help illl repond to the other queries asap -- off to my night job sad

Re: I'm confused by REST!

Okay I've decided to ajax calls that call the respective CRUD action by using RESTful urls.

i.e.

link_to_remote "Add Operative to Report", 
{:url => assignment_path (:operative_id => "4", :report_id => "1"),
:method => :post,:escape => false},

The problem is create looks for params[:assignment] whereas the initialising attributes are not sent in an 'assignment' hash.  I dont want to edit the create method as it would affect the other create forms. But then again I dont know how else to do it.