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

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…

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.

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.

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 :slight_smile:

Hm…Thanks!


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

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 :slight_smile:

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.

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 :slight_smile:

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!