Hello, I have two models associated with belongs_to and has_many. They both share some similarly named columns, for example, date. When using the Dynamic Attribute Finders for the common columns (ie: find_by_date) I get a lovely "Column: ''date'' in where clause is ambiguous:" error. I assumed that the date column that would have been used to find with would have been the date column of the model I was swinging the find_by_date off of. Does it make sense to have ambiguous columns in the where defaulting to the calling model? Or is it that my column names are not descriptive enough :) -Caleb
David Felstead
2005-Jul-14 04:02 UTC
Re: Dynamic Attribute Finders, eagerness and ambiguity
If it''s not already, ActiveRecord should be prefixing table names or table aliases to the column names in the select queries to avoid these errors... I don''t have the code with me at the moment, can anyone confirm that this is or is not the case (I''m guessing it isn''t)? Cheers! -DF On 7/14/05, Caleb Buxton <adbust-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I have two models associated with belongs_to and has_many. They both > share some similarly named columns, for example, date. > > When using the Dynamic Attribute Finders for the common columns (ie: > find_by_date) I get a lovely "Column: ''date'' in where clause is > ambiguous:" error. > > I assumed that the date column that would have been used to find with > would have been the date column of the model I was swinging the > find_by_date off of. > > Does it make sense to have ambiguous columns in the where defaulting > to the calling model? > > Or is it that my column names are not descriptive enough :) > > -Caleb > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Rafael Rezende
2005-Jul-14 21:22 UTC
Re: Dynamic Attribute Finders, eagerness and ambiguity
Rails does prefix the fields with the table name in the sql statements, so you can use: find :all, :conditions => [''my_table.date = ?'', date_obj] Maybe the problem is related to the (deprecated?) dynamic finders? 2005/7/14, David Felstead <david.felstead-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > If it''s not already, ActiveRecord should be prefixing table names or > table aliases to the column names in the select queries to avoid these > errors... I don''t have the code with me at the moment, can anyone > confirm that this is or is not the case (I''m guessing it isn''t)? > > Cheers! > > -DF > > On 7/14/05, Caleb Buxton <adbust-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hello, > > > > I have two models associated with belongs_to and has_many. They both > > share some similarly named columns, for example, date. > > > > When using the Dynamic Attribute Finders for the common columns (ie: > > find_by_date) I get a lovely "Column: ''date'' in where clause is > > ambiguous:" error. > > > > I assumed that the date column that would have been used to find with > > would have been the date column of the model I was swinging the > > find_by_date off of. > > > > Does it make sense to have ambiguous columns in the where defaulting > > to the calling model? > > > > Or is it that my column names are not descriptive enough :) > > > > -Caleb > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Rafael Rezende _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Taking a closer look at how the dynamic finders work... They basically turn find_by or find_all_by into a find :first (or all), :conditions => [''the_columns_after_by = ?'',etc] and so in splitting up the column names out of the method name they don''t prefix the column names in the condition with the appropriate table name. the dynamic finders don''t even look at the options hash very closely -- they don''t know to check to see if anything is :included Should the dynamic finder or find be responsible for prefixing table names in the where? In my mind it would make sense for the dynamic finder to take a peak at what is being included and to do the table name prefixing because part of what the user (as in developer) is doing when using the dynamic finders is saying "look, you take care of these conditions" However in this case it isn''t really fully taking care of the conditions. Thoughts? On 7/14/05, Rafael Rezende <rrezende-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Rails does prefix the fields with the table name in the sql statements, so > you can use: > > find :all, :conditions => [''my_table.date = ?'', date_obj] > > Maybe the problem is related to the (deprecated?) dynamic finders? > > 2005/7/14, David Felstead <david.felstead-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > If it''s not already, ActiveRecord should be prefixing table names or > > table aliases to the column names in the select queries to avoid these > > errors... I don''t have the code with me at the moment, can anyone > > confirm that this is or is not the case (I''m guessing it isn''t)? > > > > Cheers! > > > > -DF > > > > On 7/14/05, Caleb Buxton <adbust-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, > > > > > > I have two models associated with belongs_to and has_many. They both > > > share some similarly named columns, for example, date. > > > > > > When using the Dynamic Attribute Finders for the common columns (ie: > > > find_by_date) I get a lovely "Column: ''date'' in where clause is > > > ambiguous:" error. > > > > > > I assumed that the date column that would have been used to find with > > > would have been the date column of the model I was swinging the > > > find_by_date off of. > > > > > > Does it make sense to have ambiguous columns in the where defaulting > > > to the calling model? > > > > > > Or is it that my column names are not descriptive enough :) > > > > > > -Caleb > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > -- > Rafael Rezende > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >