Hi, I am working with an existing database that has table and field names such as 'TblUsers' and 'txtEmailAddress'. Is rails sensitive to the case of the letters here? or can I simply refer to these tables and fields as 'tblusers' and 'txtemailaddress' in my controllers ad rhtml files? Also, the database has id fields named after the tables such as 'TblUsersID' for 'TblUsers'. I know rails prefers id fields to be named 'id'. How difficult is it to override this? If I can override it, does every instance of 'id' become 'TblUsersID'? eg. in the following example, would this be correct? <form action="../update/<%= @tbluser.tbluserid %>" method="POST""> <input tbluserid="tbluser_tbluserid" name="tbluser[tbluserid]" size="30" type="hidden" value="<%= @tbluser.tbluserid %>" /> <p><b>Title</b><br> <input id="tbluser_title" name="tbluser[title]" size="30" type="text" value="<%= @tbluser.title %>" /> </p> Thank you in advance! Alana
on 2006-04-07 14:02
on 2006-04-07 14:52
On Friday 07 April 2006 12:02, Alana wrote: > If I can override it, does every instance of 'id' become 'TblUsersID'? > > Thank you in advance! > > Alana > In every model, you can execute set_primary_key. So, for your 'TblUsers', you can do set_primary_key(:TblUsersID) When you have done that, you can call .id on your objects, because it maps to the primary key. Calling "tbluserid" as you did in your example is not necessary (it won't even work I guess, mostly because it should be TblUsersID...), the primary key is always "id". And, it is indeed case sensitive. You could have tried that out easily :) BTW, you don't have to specify the HTML tags for the form and fields yourself. If you make use of the helpers (like "text_field"), it creates the necessary HTML for you. Also, if you adhere to the standard naming convention, handling the data becomes much easier. The text_field method for example takes as the first two arguments the object and the method. If for example you use "post" and object and "title" as method, it will create a textfield post[title]. This way, when you have a lot of textfields for the object post, you can simply do "post.attributes = @params["post"]", to have them assigned all at once. But, I guess the latter you already knew, looking at your code. See http://api.rubyonrails.com/ for more info on the details of the methods I mentioned.
on 2006-04-08 17:07
On Fri, Apr 07, 2006 at 12:02:28PM +0200, Alana wrote: > I am working with an existing database that has table and field names > such as 'TblUsers' and 'txtEmailAddress'. Is rails sensitive to the case > of the letters here? or can I simply refer to these tables and fields as > 'tblusers' and 'txtemailaddress' in my controllers ad rhtml files? Unfortunately, Rails is sensitive to the case of table and field names. I recently developed an application that had to deal with legacy table and field names, and also had to work with Postgres databases created with all lowercase names, and with Mysql databases created with studlycased names. I ended up developing my application to work against Postgres, and then adopting this code to work with Mysql by redefining the table names and accessors when a Mysql database was in use. This involved augmenting each model definition, as for instance: class Jobmedia < ActiveRecord::Base Jobmedia.table_name = 'jobmedia' Jobmedia.primary_key = 'jobmediaid' if STUDLY Jobmedia.table_name = 'JobMedia' def volumename; self.VolumeName; end ... end end ... and then determining if studlification was required in environment.rb: STUDLY = ActiveRecord::Base.configurations[RAILS_ENV]['adapter'] == 'mysql' It would be very handy if the Active Record framework provided some type of "downcase fieldnames" option when generating the setter and getter methods, but this approach got the job done for me. Best of luck. -- John Kodis.