# Newbie question/help 2 tables, 'sync' cell values

**URL:** <https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985>\
**Category:** Rails\
**Created:** [December 23, 2005, 1:36pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985 "2005-12-23T13:36:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandru\_Andrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@Alexandru\_Andrei](https://www.ruby-forum.com/u/Alexandru_Andrei)\
**Post date:** [December 23, 2005, 1:36pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985/1 "2005-12-23T13:36:40Z")

</div>

I have to \<\< search for each problems.room\_id=rooms.id and do  
problems.room\_no=rooms.room \>\>

HOW?

More details:

I have the following tables:

- a “rooms” table, with fields (id,room,create…)
- a “problems” table with fields (id,room\_id,room\_no, …)

problem.rb has  
belongs\_to :room  
validates\_associated :room

and room.rb has  
validates\_length\_of :room, :within =\> 1…20  
validates\_uniqueness\_of :room, :message =\> “already exists!”  
has\_many :problems

For “problems” in the edit/add form (\_form partial) I have a drop down  
that named “Room no” that actually sets the value for room\_id in the  
problems table.

How can I set the value of room\_no field in the ‘problems’ table (to be  
the ‘room’ field in rooms?

Thank you, I hope this make sense…

---

<div class="post-metadata">

**Author:** ![Alexandru\_Andrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@Alexandru\_Andrei](https://www.ruby-forum.com/u/Alexandru_Andrei)\
**Post date:** [December 23, 2005, 2:46pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985/2 "2005-12-23T14:46:20Z")

</div>

Alexandru Andrei wrote:

> I have to \<\< search for each problems.room\_id=rooms.id and do  
> problems.room\_no=rooms.room \>\>  
> …  
> How can I set the value of room\_no field in the ‘problems’ table (to be  
> the ‘room’ field in rooms?

Why do you need the room\_no attribute in Problems? It can be accessed  
as  
problem.room.room (which you probably should rename as  
problem.room.number).  
Is it a legacy schema?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Alexandru\_Andrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@Alexandru\_Andrei](https://www.ruby-forum.com/u/Alexandru_Andrei)\
**Post date:** [December 23, 2005, 3:39pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985/3 "2005-12-23T15:39:19Z")

</div>

Mark Reginald J. wrote:

> Why do you need the room\_no attribute in Problems? It can be accessed  
> as  
> problem.room.room (which you probably should rename as  
> problem.room.number).  
> Is it a legacy schema?

> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Alexandru\_Andrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@Alexandru\_Andrei](https://www.ruby-forum.com/u/Alexandru_Andrei)\
**Post date:** [December 23, 2005, 3:41pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985/4 "2005-12-23T15:41:39Z")

</div>

Mark Reginald J. wrote:

> Why do you need the room\_no attribute in Problems? It can be accessed  
> as  
> problem.room.room (which you probably should rename as  
> problem.room.number).  
> Is it a legacy schema?  
> I am not sure what “legacy schema” really means!

Well, firts of all I need it for listing the problems… And, later on  
(the truth is that I trying to re-write a php/mysql app in RoR (just for  
learning purposes) but I wanted to have the original tables layouts (  
they were already RubyLike made… without any knowledge of Ruby 🙂

Hm…Thanks!

> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Alexandru\_Andrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@Alexandru\_Andrei](https://www.ruby-forum.com/u/Alexandru_Andrei)\
**Post date:** [December 23, 2005, 4:13pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985/5 "2005-12-23T16:13:44Z")

</div>

Alexandru Andrei wrote:

> Mark Reginald J. wrote:
> 
> > Is it a legacy schema?
> 
> I am not sure what “legacy schema” really means!
> 
> Well, firts of all I need it for listing the problems… And, later on  
> (the truth is that I trying to re-write a php/mysql app in RoR (just for  
> learning purposes) but I wanted to have the original tables layouts (  
> they were already RubyLike made… without any knowledge of Ruby 🙂

Are you willing to change fields in the MySQL tables of the app so that  
it works better with the RoR version, or do you want leave the database  
alone so that the PHP version still works?

If the former: To list a problem with its room number, just add the  
“:include =\> room” option when you call problem = Problem.find, and  
then you can render the room number with problem.room.room.

If the latter, you can write a before\_save method in Problem that  
copies the value of room.room to room\_no:

def before\_save  
self.room\_no = room ? room.room : nil  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Alexandru\_Andrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@Alexandru\_Andrei](https://www.ruby-forum.com/u/Alexandru_Andrei)\
**Post date:** [December 23, 2005, 6:36pm UTC](https://www.ruby-forum.com/t/newbie-question-help-2-tables-sync-cell-values/50985/6 "2005-12-23T18:36:24Z")

</div>

Mark Reginald J. wrote:

> Are you willing to change fields in the MySQL tables of the app so that  
> it works better with the RoR version, or do you want leave the database  
> alone so that the PHP version still works?
> 
> If the former: To list a problem with its room number, just add the  
> “:include =\> room” option when you call problem = Problem.find, and  
> then you can render the room number with problem.room.room.

Add that in the controller? (sorry, I started reading about RoR  
yesterday 🙂

> If the latter, you can write a before\_save method in Problem that  
> copies the value of room.room to room\_no:
> 
> def before\_save  
> self.room\_no = room ? room.room : nil  
> end

I want to get(print) the room name from the rooms table, based on the  
room\_id from the problems table. (SQL: where problems.room\_id = room.id)

I was trying something like

```
<% for problem in @problems %>
  
	<%=h room[:name].find(Room[problem[:room_id]]) %>

(but i am aware that it doesn’t really make sense!
```
