Home > Software engineering >  Arel table query for where -> has_one through
Arel table query for where -> has_one through

Time:06-09

I am trying to use an arel table query to perform a 'where' query on a has_one relationship. The has_one relationship is necessary as it's currently set up as a 'has_one_through'.

The reason for using an arel table query is that this query logic will be dynamic. There are several has_one relationships that need to be queried separately so I want to be as dry and scalable as possible.

The query I am trying to replicate looks a little like this.

Query I want to replicate ('style/s' keyword would be dynamic)

Book.includes(:style).where(styles: {id: 8})

What I've tried so far

object = Style.find(8)
object_key = object.model_name.i18n_key
object_table = Arel::Table.new(object_key)
has_one = object_table[:id].eq(object.id)

Book.includes(object_key).where(has_one)

CodePudding user response:

You can achieve this without using Arel by using pluralize on the object_key

object = Style.find(8)
object_key = object.model_name.i18n_key

Book.includes(object_key).where(object_key.to_s.pluralize => { id: object.id })

If you want to use Arel anyway you can do next:

object = Style.find(8)
object_key = object.model_name.i18n_key
object_table = Arel::Table.new(object_key.to_s.pluralize)
Book.joins(object_key).where(object_table[:id].eq(object.id))
  • Related