views:

17

answers:

1

I'm trying to build a form for an "Incident" that allows users to add notes (text fields) to it dynamically with some javascript. So when they click an "Add Note" button in the form, a text field pops up and they can add a note. If they click it again, another field will show up. So far this works fine when creating a new incident, but when I edit the incident and add a new field, it doesn't pick up the relationship between incident_note and user.

For example, here is what I see when I create a new incident.

 INSERT INTO "incident_notes" ("created_at", "updated_at", "user_id", "note", "incident_id") VALUES('2010-07-02 14:07:42', '2010-07-02 14:07:42', 2, 'A Note', 8)

As you can see, the user_id field has a number assigned to it. But here is what happens during the edit when I add another note:

INSERT INTO "incident_notes" ("created_at", "updated_at", "user_id", "note", "incident_id") VALUES('2010-07-02 14:09:11', '2010-07-02 14:09:11', NULL, 'Another note', 8)

The user_id is NULL. I'm not sure what I've done. The code is very similar for both "edit" and "new" in the controller.

I have the following models and relationships (only showing the relevant sections):

class Incident < ActiveRecord::Base
  has_many                :incident_notes
  belongs_to              :user
end

class IncidentNote < ActiveRecord::Base
  belongs_to :incident
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :incidents
  has_many :incident_notes
end

Here is the relevant part of the new form (the edit is essentially the same):

<% form_for([@customer,@incident]) do |f| %>
  <p>
    <% f.fields_for :incident_notes do |inf| %>
      <%= render "incident_note_fields", :f => inf %>
    <% end %>
    <p><%= link_to_add_fields "Add Note", f, :incident_notes %></p>
  </p>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

And here are the create and update methods in the Incident controller.

def create
  @incident = @customer.incidents.build(params[:incident])
  @incident.capc_id = generate_capc_id
  for inote in @incident.incident_notes
    (inote.user = current_user) if (inote.user == nil)
  end

  respond_to do |format|
    if @incident.save #etc
end

def update
  @incident = @customer.incidents.find(params[:id])
  for inote in @incident.incident_notes
    (inote.user = current_user) if (inote.user == nil)
  end

  respond_to do |format|
    if @incident.update_attributes(params[:incident])
    #etc
end

There may be a better way to do this, but as you can see in the "create" method I had to manually set the incident_note user field to the current user. This works fine, but the same does not seem to work in the update method.

Any ideas, suggestions, and help will be much appeciated! I'm very stuck at the moment. :)

A: 

I suggest that you don't have incident_notes directly belonging to user. In other words a user has many incidents and an incident has many incident notes.

class Incident < ActiveRecord::Base
  has_many                :incident_notes
  belongs_to              :user
end

class IncidentNote < ActiveRecord::Base
  belongs_to :incident
end

class User < ActiveRecord::Base
  has_many :incidents
  has_many :incident_notes, :through => :incident
end

The user's incident notes are then obtained through her incidents model

bjg
The problem with this is I want a user to be able to create notes for incidents owned by other users. If I understand you correctly, this won't allow that, right?
Magicked
No, I believe that would still work provided the ownership of the incident stays with the original user. The question as to whether user B "has permission" to update or create notes for user A is an application specific concept and would be enforced by the application not through the database schema. So long as you allow the controller actions to permit "current_user" to find incidents belonging to another user you should be ok
bjg