searchlogic icon indicating copy to clipboard operation
searchlogic copied to clipboard

Search through association with column named "name" fails

Open naven87 opened this issue 16 years ago • 1 comments

Take the following models Earnings: belongs_to :period Period: has_many :earnings if Period has a string column titled "name" the following search fails Earnings.period_name_like("Q309") with a MySQL error that the Table/Alias periods is not unique. If I look at the generated MySQL, it creates both an inner and and outer join on the periods table against the earnings table.

If I instead call Earning.period_id_is(7) it works fine and no error is generated. The MySQL query only includes one outer join and no inner join.

naven87 avatar Oct 14 '09 05:10 naven87

So after more digging, I found that the column name was irrelevant, the real issue was any default scope on the model is merged into the query without concern for overlap. So if the default scope has a join or include in it, that will be included directly in the final query and if the searchlogic portion uses the same table name it will add it's own join into the query which can create the problem described

naven87 avatar Oct 14 '09 05:10 naven87