Forum: Ruby on Rails Updating Table without ID Column

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam J. (Guest)
on 2006-03-17 03:40
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!
Clint P. (Guest)
on 2006-03-17 03:50
(Received via mailing list)
Hey Adam. Would you send the code of your method along? That will make
it easier to help.

THanks
Clint
Adam J. (Guest)
on 2006-03-17 04:21
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
Adam J. (Guest)
on 2006-03-30 01:55
Still no luck here. Really got me stumped.

Any hints would really help!
This topic is locked and can not be replied to.