Bob O
2009-Sep-07 19:45 UTC
ActiveRecord find on legacy database having enum columns problem
hello, Im interfacing a PHP app with my rails app and one of the tables im trying to do a find on has an enum column. So when i run the find, its adding an extra condition. This is my first time interfacing with a DB like this so im not quite sure how to remedy my problem This is my named_scope named_scope :ads, lambda { |advertiser| { :conditions => ["advertiser_id = ? && type = ?", advertiser, "advertisement"]} this is what i get in the terminal SELECT * FROM `broadcast` WHERE (advertiser_id = ''5'' && type ''advertisement'') AND ( (`broadcast`.`type` = ''Broadcast'' ) ) so its appending the (`broadcast`.`type` = ''Broadcast'' ). There is a type column in the DB that is an enum column. any help?
Colin Law
2009-Sep-07 21:33 UTC
Re: ActiveRecord find on legacy database having enum columns problem
2009/9/7 Bob O <sngndncmn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > hello, > > Im interfacing a PHP app with my rails app and one of the tables im > trying to do a find on has an enum column. So when i run the find, its > adding an extra condition. This is my first time interfacing with a DB > like this so im not quite sure how to remedy my problem > > This is my named_scope > named_scope :ads, lambda { |advertiser| { :conditions => > ["advertiser_id = ? && type = ?", advertiser, "advertisement"]} > > this is what i get in the terminal > > SELECT * FROM `broadcast` WHERE (advertiser_id = ''5'' && type > ''advertisement'') AND ( (`broadcast`.`type` = ''Broadcast'' ) ) > > so its appending the (`broadcast`.`type` = ''Broadcast'' ). There is a > type column in the DB that is an enum column.The problem is that the column is named ''type''. This is a reserved word in rails as it used in Single Table Inheritance. If you can change the name of the column then that should sort it. If you can''t change it then http://www.benlog.org/2007/1/16/legacy-rails-beware-of-type-columns provides a work around. Colin
Bob O
2009-Sep-07 22:23 UTC
Re: ActiveRecord find on legacy database having enum columns problem
Excellent. Thank you for the link. That worked great! On Sep 7, 3:33 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/9/7 Bob O <sngndn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > hello, > > > Im interfacing a PHP app with my rails app and one of the tables im > > trying to do a find on has an enum column. So when i run the find, its > > adding an extra condition. This is my first time interfacing with a DB > > like this so im not quite sure how to remedy my problem > > > This is my named_scope > > named_scope :ads, lambda { |advertiser| { :conditions => > > ["advertiser_id = ? && type = ?", advertiser, "advertisement"]} > > > this is what i get in the terminal > > > SELECT * FROM `broadcast` WHERE (advertiser_id = ''5'' && type > > ''advertisement'') AND ( (`broadcast`.`type` = ''Broadcast'' ) ) > > > so its appending the (`broadcast`.`type` = ''Broadcast'' ). There is a > > type column in the DB that is an enum column. > > The problem is that the column is named ''type''. This is a reserved > word in rails as it used in Single Table Inheritance. If you can > change the name of the column then that should sort it. If you can''t > change it thenhttp://www.benlog.org/2007/1/16/legacy-rails-beware-of-type-columns > provides a work around. > > Colin