-
-
Save amiel/1331837 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Asset < AR | |
end | |
class Alert < AR | |
belongs_to :asset | |
has_many :logs | |
accepts_nested_attributes_for :logs | |
end | |
class Log < AR | |
belongs_to :asset | |
belongs_to :alert | |
end | |
###### zee controller | |
class AlertsController < ApplicationController | |
def resolve | |
# TODO: only alerts that need resolution | |
respond_with { | |
@alert = @asset.alerts.find(params[:id]) | |
@alert.log_entries.build({:completed_at => Time.now}) | |
} | |
end | |
def update | |
@alert = @asset.alerts.find(params[:id]) | |
@alert.update_attributes(params[:alert]) | |
respond_with(@alert, :location => asset_alerts_path(@asset.becomes(Asset))) | |
end | |
end | |
##### zee view | |
<%= semantic_form_for([@asset.becomes(Asset), @alert.becomes(Alert)]) do |f| %> | |
<%= f.inputs do %> | |
<%= f.semantic_fields_for(:log_entries, @alert.log_entries.last) do |log| %> | |
<%=log.input :completed_at, :as => :sane_datetime %> | |
<%=log.input :content %> | |
<% end %> | |
<%= f.input :next_due %> | |
<% end %> | |
<%=f.buttons %> | |
<% end %> | |
##### what I post: | |
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FOOBAR!!!!111one", "alert"=>{"log_attributes"=>{"0"=>{"completed_at(1s)"=>"11/01/2011", "completed_at(2s)"=>"12:00 AM", "content"=>"TESTERY!"}}, "next_due"=>"100"}, "commit"=>"Update Alert", "asset_id"=>"1", "id"=>"1"} | |
##### what I get: | |
=> #<Log id: 1, asset_id: nil, issue_id: nil, alert_id: 1, cost: nil, content: "TESTERY!", completed_at: "2011-11-01 07:00:00", created_at: "2011-11-01 20:25:15", updated_at: "2011-11-01 20:25:15"> | |
Any way to set asset_id on the child created log? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Alert < AR | |
belongs_to :asset | |
has_many :logs | |
accepts_nested_attributes_for :logs | |
def log_entries_attributes=(attributes) | |
# Manipulate attributes | |
psuedocode do | |
attributes.each { |a| a[:asset_id] = self.asset_id } | |
end | |
# This method could fail strangely if asset_id is set after log_entries_attributes | |
# For example: | |
# alert.asset_id = 1 | |
# alert.update :log_entries_attributes => [...], :asset_id => 2 | |
# In this case, I think that log entries will have asset_id = 1 | |
super attributes | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Alert < AR | |
belongs_to :asset | |
has_many :logs | |
accepts_nested_attributes_for :logs | |
def asset_id=(id) | |
# TODO: Make sure that logs are auto-saved when we save... | |
logs.each { |l| l.asset_id = id } | |
# This might not work as expected if there are logs that are | |
# created after asset_id is set | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment