Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8422607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:30:00+00:00 2026-06-10T03:30:00+00:00

Basically I have a One-To Many relationship between Alert and Updates. Many Updates belong

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:30:02+00:00Added an answer on June 10, 2026 at 3:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one-to-many relationship between Part and Params (a Part has many Params).
I have a one to many relationship between two tables. The many table contains
I basically have a program that filters records from one excel file to another
have one time consuming step that flattens a bunch of files. basically i'd like
I have 2 entities, A and B that have a many to many relationship.
I have one line of code which seems commented. Basically the thing I want
I have a one-page OpenOffice document which is a POD template. Basically, I use
Basically I have some class objects, each with three properties. Once one class object
Basically, I have a list of delivery checkboxes one for deliver to this address
Basically, I have the same question as this one , which unfortunately never got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.