I was stuck on this for a while, and the answer was not what I expected. This is just a "heads up" - perhaps someone can tell me if I should have known the answer before hand? Attributes on join tables are always returned as type string - it doesn''t matter what they are in the database. At least that is the case for type int on mysql. It''s not a big deal, once you know what to expect. Example: You have a students table and an events table. The event_student table has arrival_time and departure_time. A student has many events, an event has many students. For mysql, the create syntax for the event_student table would be: create table event_student ( event_id int, student_id int, number_just_for_fun int ) class test_student #Some fixtures to put data in - assume that student 1 attends event 1 #with number_just_for_fun = 5 on the join table record def test_general student = Student.find(1) association = student.events.find(1) s = association.number_just_for_fun.class.name puts ''class type: '' + s end end When you run this test, it will output: class type: String The value is correct, it is 5 in this case, but 5 as a String, not a Fixnum as I expected. Is this behaviour documented anywhere?
There is no arrival_time and departure_time - I simplified the example and just have an int for the association attribute. I changed things part way through writing the message and forgot to change that. Sorry if that caused confusion. Matthew Thill wrote:> I was stuck on this for a while, and the answer was not what I expected. > This is just a "heads up" - perhaps someone can tell me if I should have > known the answer before hand? > > Attributes on join tables are always returned as type string - it > doesn''t matter what they are in the database. At least that is the case > for type int on mysql. It''s not a big deal, once you know what to expect. > > Example: > You have a students table and an events table. The event_student table > has arrival_time and departure_time. A student has many events, an event > has many students. For mysql, the create syntax for the event_student > table would be: > > create table event_student ( > event_id int, > student_id int, > number_just_for_fun int > ) > > class test_student > #Some fixtures to put data in - assume that student 1 attends event 1 > #with number_just_for_fun = 5 on the join table record > def test_general > student = Student.find(1) > association = student.events.find(1) > s = association.number_just_for_fun.class.name > puts ''class type: '' + s > end > > end > > When you run this test, it will output: > > class type: String > > The value is correct, it is 5 in this case, but 5 as a String, not a > Fixnum as I expected. Is this behaviour documented anywhere? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails