Updating Table without ID Column


#1

Hey All,

I’ve been having a slight problem updating attributes for a row that
matches something other than it’s ID…For ex:

My table that has 3 rows…ColumnID, ColumnTitle, and ColumnDescription.
I want to update a row in that table WHERE CoumnTitle = @myvar…But
whenever i run update_attrbutes[:mytable] it’s looking for an id for the
record when i need to to look for the title…is there a way to override
that?

Any help is much appreciated!


#2

Hey Adam. Would you send the code of your method along? That will make
it easier to help.

THanks
Clint


#3

Clint P. wrote:

Hey Adam. Would you send the code of your method along? That will make
it easier to help.

THanks
Clint

Sure…Something like this:

The view is just a form with the 3 rows (ColumnID, ColumnTitle, and
ColumnDescription), I specify the value for ColumnID in a hidden field
(i get the value from the URL).

Controller is a basic update method:

#controller
def save_form
@column = Columns.find_by_ColumnID(params[:id])
if @column.update_attributes(params[:column])
flash[:notice] = ‘Column was successfully updated.’
redirect_to :action => ‘show’, :id => @column
else
render :action => ‘edit’
end
end

When it executes save_form is notifies me there is no id column in
UPDATE…WHERE id = NULL, but i want it to update where ColumnID =
hidden ColumnID value from the form.

It’s pretty basic, if i could only specify the update code it would be
simple.

Instead of it running UPDATE Table SET ColumnTitle = ColumnTitle WHERE
id = NULL

It needs to run:

UPDATE Table SET ColumnTitle = ColumnTitle WHERE ColumnID = ColumnID


#4

Still no luck here. Really got me stumped.

Any hints would really help!