Reputation: 45
Basically I have a One-To Many relationship between Alert and Updates. Many Updates belong to one Alert. Now my problem is that I cannot update any records in my database. I can create a new record without problems. Rails logger prints me the actual record to the logs. As you can see from the log files @new_alert retrieves the changed record, which should be saved to the database. Although it says "Alert was succesfully updated" it doesn't update the old record with the new one. When I remove the relationship between Alert and Update then it works perfectly. I guess I have missed something in the Alert or Update model.
alerts_controller.rb ...
def update
@alert = Alert.find(params[:id])
@new_alert = params[:alert]
Rails.logger.debug "alert_params: #{@new_alert.inspect}"
Rails.logger.debug "alert_db: #{@alert.inspect}"
respond_to do |format|
if @alert.update_attributes(params[:alert])
format.html { redirect_to @alert, notice: 'Alert was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @alert.errors, status: :unprocessable_entity }
end
end
end...
Alert.rb
class Alert < ActiveRecord::Base
has_many :update
accepts_nested_attributes_for :update
end
Update.rb
class Update < ActiveRecord::Base
belongs_to :alert, :foreign_key => 'alert_id'
end
(MySQL) alerts table:
CREATE TABLE `alerts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`status_id` int(11) DEFAULT NULL,
`date` datetime DEFAULT NULL,
`text` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`update_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
(MySQL) updates table:
CREATE TABLE `updates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`alert_id` int(11) DEFAULT NULL,
`text` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
Log output:
Started PUT "/alerts/4" for 127.0.0.1 at 2012-08-24 15:47:12 +0200
Processing by AlertsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GHM+S34PV4o46SpZZm67+mM8Lu9eY/BiWGMjDpwju9c=", "alert"=>{"title"=>"afgaegafga56556", "text"=>"afgafgafgarg", "status_id"=>"2"}, "commit"=>"Update Alert", "id"=>"4"}
[1m[35mAlert Load (0.5ms)[0m SELECT `alerts`.* FROM `alerts` WHERE `alerts`.`id` = 4 LIMIT 1
alert_params: {"title"=>"afgaegafga56556", "text"=>"afgafgafgarg", "status_id"=>"2"}
alert_db: #<Alert id: 4, status_id: 2, date: "2012-08-22 20:00:19", text: "afgafgafgarg", title: "afgaegafga", update_id: 1>
[1m[36m (0.0ms)[0m [1mBEGIN[0m
[1m[35mUpdate Load (0.5ms)[0m SELECT `updates`.* FROM `updates` WHERE `updates`.`alert_id` = 4
[1m[36m (0.5ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/alerts/4
Completed 302 Found in 4ms (ActiveRecord: 1.5ms)
Thanks!
Upvotes: 0
Views: 451
Reputation: 3779
has_many :update
should be plural: has_many :updates
otherwise the relation probably won't work.
The same is true for accepts_nested_attributes_for
.
Also (not significant but maybe good to know) you don't have to define a foreign key when it matches the relationship name. So belongs_to :alert, :foreign_key => 'alert_id'
is exactly the same as belongs_to :alert
.
Upvotes: 2