ActiveRecord default alias for model

I am using cancan to define some abilities - and I need to specify the SQL WHERE clause. This sql need to use an alias to the SELECT

eg The default select through the model is “SELECT * from appliances” but I need to make it “SELECT * from appliances ap” so I can use the “ap” alias in the cancan SQL restriction

My question is, can I add this model/table alias somehow so by default all the model access SQL has FROM [TABLENAME] [ALIAS]

I know you can do it dynamically, but I need to set a default if at all possible

OK - for the purposes of cancan … I added a new from_clause to the default active record relation before it made any queries … and that worked

@appliance_manufacturers.from_clause = ActiveRecord::Relation::FromClause.new(“appliance_manufacturers am”, “ApplianceManufacturers”)

Even better - just add an alias for the table

class ApplianceManufacturer < ApplicationRecord
ApplianceManufacturer.arel_table.table_alias = “am”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

Sponsor our Newsletter | Privacy Policy | Terms of Service