Alexandru Andrei
2005-Dec-23 12:36 UTC
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... -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2005-Dec-23 13:45 UTC
Re: Newbie question/help 2 tables, ''sync'' cell values
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.
Alexandru Andrei
2005-Dec-23 14:39 UTC
Re: Newbie question/help 2 tables, ''sync'' cell values
Mark Reginald James 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.-- Posted via http://www.ruby-forum.com/.
Alexandru Andrei
2005-Dec-23 14:41 UTC
Re: Newbie question/help 2 tables, ''sync'' cell values
Mark Reginald James 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.-- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2005-Dec-23 15:11 UTC
Re: Newbie question/help 2 tables, ''sync'' cell values
Alexandru Andrei wrote:> Mark Reginald James 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.
Alexandru Andrei
2005-Dec-23 17:36 UTC
Re: Newbie question/help 2 tables, ''sync'' cell values
Mark Reginald James 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 > endI 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 <pre> <% for problem in @problems %> <tr> <td><%=h room[:name].find(Room[problem[:room_id]]) %></td> </pre> (but i am aware that it doesn''t really make sense! -- Posted via http://www.ruby-forum.com/.