Skip to content

Tag: ruby-on-rails

Rails: How to use instance method in Active Record Query

I am having a filter query which should return all the records where either attribute (column) “status” is not “done”, or instance method “completeness_status” does not return “done” The query is something like that: Studies.where(“studies.status != ? OR studies.completeness_status != ?”, “done”, “done”) but I am getting error that column completeness_status does not exist. Unfortunately, the column status is not

Rails: better approach in handling dynamic column export feature

Given a dynamic export feature where users could select a column they would want to include in an export, some columns are an N+1, and some require multiple joining of tables. Ex: Tables: Columns to export in UI: Customer Name, Full Address, Pets Sample result: There are 2 ways that I could think of to achieve this in rails, first

Rails: Finding records where a nested association is empty

In a Rails application I’m working on, I’ve got a few different models associated like this (condensed for clarity): group.rb group_member.rb newsletter.rb newsletter/author.rb newsletter/story.rb Given the above associated models, here’s the framework I’m working within: Each Newsletter has n Authors (Group Members) and n Newsletters. Each Group Member can author multiple stories for a given newsletter. Each story is one

Append additional conditions to a query

I have a query where I need to add several conditions as an OR clause, because I want to use LIKE, so I can not use IN instead. So how can I add multiple OR from an array? The SQL should look like this: where ‘a’, ‘b’, ‘c’ is from an array. Answer From the Rails API docs, the .or

ActiveRecord having count query condition

I have 2 models : Conversation and Message I need to retrieve all conversations that belongs to a Brand and where there are at least one message from the brand and one message from an influencer This is my query : But i get conversations that doesn’t have messages from both… What am i doing wrong please ? Answer The