Can this be done as a Virtual Column?

Hi,

I need to be able to query on my full_name method. As I know you can’t do that I have seen Rails 7 new virtual column support. Is it possible to convert my current method into a virtual column or should I use an after save callback to update a column called full_name?

    def full_name
        parts = [name]

        parts << "(#{position})" if position.present?
        parts << "(#{version_name})" if (has_versions? && version_name.present?)

        parts.join(" ")
    end

This looks a bit too complex for a postgres generated column. Best option if to create a full_name column and use a before_save callback to avoid the extra query. That’s what I do on models I have full mames and similar

1 Like

I thought that would be the case but wanted to be sure first.

Thankyou!