# Cn w update the two tables under one action ...both are in one to one relation

**URL:** <https://www.ruby-forum.com/t/cn-w-update-the-two-tables-under-one-action-both-are-in-one-to-one-relation/130567>\
**Category:** Rails\
**Created:** [February 29, 2008, 1:38pm UTC](https://www.ruby-forum.com/t/cn-w-update-the-two-tables-under-one-action-both-are-in-one-to-one-relation/130567 "2008-02-29T13:38:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [February 29, 2008, 1:38pm UTC](https://www.ruby-forum.com/t/cn-w-update-the-two-tables-under-one-action-both-are-in-one-to-one-relation/130567/1 "2008-02-29T13:38:39Z")

</div>

i hav tried this thng…but it s coming to the else part.

class Article \< ActiveRecord::Base  
belongs\_to :category  
end

class Category \< ActiveRecord::Base  
has\_one :article  
end

def update  
@article = Category.new  
@article.uploaded\_data = (params[:article])  
@article.article = Article.find(params[:id])  
@article.article.tag\_list = (params[:tag\_list])  
if @article.update\_attributes(params[:article])  
redirect\_to :action =\> ‘show’, :id =\> @article  
else  
@categories = Category.find(:all)  
render :action =\> ‘edit’

end

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [February 29, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/cn-w-update-the-two-tables-under-one-action-both-are-in-one-to-one-relation/130567/2 "2008-02-29T15:31:25Z")

</div>

joe wrote:

> def update  
> @article = Category.new  
> @article.uploaded\_data = (params[:article])  
> @article.article = Article.find(params[:id])  
> @article.article.tag\_list = (params[:tag\_list])  
> if @article.update\_attributes(params[:article])  
> redirect\_to :action =\> ‘show’, :id =\> @article  
> else  
> @categories = Category.find(:all)  
> render :action =\> ‘edit’
> 
> end

Instead of update\_attributes(), use update\_attributes!() and then you  
will be able to see the exception of why you weren’t able to save…

optionally, you can just check @article.errors…

hth

ilan

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [February 29, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/cn-w-update-the-two-tables-under-one-action-both-are-in-one-to-one-relation/130567/3 "2008-02-29T16:31:47Z")

</div>

joe wrote:

> ```
> @article = Category.new
> 
> ```

So @article is a Category. It may help to follow what is happening by  
using more relevant variable names. Perhaps use @category to refer to a  
category?

> @article.uploaded\_data = (params[:article])

What is params[:article] refering to? Does it come from a form editing  
an article or is it just a text field which you’re setting to this  
attribute of a Category object?

> ```
> @article.article = Article.find(params[:id])
> @article.article.tag_list = (params[:tag_list])
> if @article.update_attributes(params[:article])
> 
> ```

You’ve been carefully setting attributes of @article (a Category object)  
and now you are going to overwrite those from params[:article] which  
earlier looked like a text element (uploaded\_data) but now is a hash of  
Category object keys and values?

I suspect your problem is that you are trying to set this Category  
object (@article) from an Article attribute hash and you probably want:

if @article.article.update\_attributes(params[:article])

?  
If so, then the confusion is probably due to calling your Category  
object by the name @article
