Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predicate search breaks after upgrading to v1.2.2 #34

Closed
madhums opened this issue Jan 2, 2024 · 1 comment
Closed

predicate search breaks after upgrading to v1.2.2 #34

madhums opened this issue Jan 2, 2024 · 1 comment

Comments

@madhums
Copy link

madhums commented Jan 2, 2024

Hi, I updated mobility-ransack to 1.2.2 which also updates ransack to 4.1.1. After adding the ransackable_attributes and associations, I expect it to work like before but now predicate search stops working.

I am not sure if this is an upstream issue or something related to this plugin.

# Story class
class Story < ApplicationRecord
  extend Mobility
  has_many :story_updates, -> { kept.order(created_at: :asc) }, dependent: :destroy, inverse_of: :story
  translates :title, :description, :outcomes, :source

  def self.ransackable_attributes(auth_object = nil)
    %w[title, description, outcomes, source, country]
  end

  def self.ransackable_associations(auth_object = nil)
    %w[story_updates]
  end
end

# StoryUpdate class
class StoryUpdate < ApplicationRecord
  extend Mobility
  belongs_to :story

  translates :title, :description

  def self.ransackable_attributes(auth_object = nil)
    %w[title description]
  end
end

Detailed Logs:

ActionView::Template::Error:
       undefined method `title_or_description_or_outcomes_or_source_or_story_updates_title_i_cont_any' for Ransack::Search<class: Story, base: Grouping <combinator: and>>:Ransack::Search
     # ./app/views/stories/_search.html.erb:5:in `block in _app_views_stories__search_html_erb__1766051873175227570_75060'
     # ./app/views/stories/_search.html.erb:1:in `_app_views_stories__search_html_erb__1766051873175227570_75060'
     # ./app/views/stories/index.html.erb:5:in `_app_views_stories_index_html_erb___3856470445803634[16](https://github.com/compassionprojects/socialchange/actions/runs/7389850038/job/20103545544#step:8:17)1_75040'
     # ./spec/requests/stories_spec.rb:7:in `block (3 levels) in <main>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `title_or_description_or_outcomes_or_source_or_story_updates_title_i_cont_any' for Ransack::Search<class: Story, base: Grouping <combinator: and>>:Ransack::Search
     #   ./app/views/stories/_search.html.erb:5:in `block in _app_views_stories__search_html_erb__[17](https://github.com/compassionprojects/socialchange/actions/runs/7389850038/job/20103545544#step:8:18)6605[18](https://github.com/compassionprojects/socialchange/actions/runs/7389850038/job/20103545544#step:8:19)73175[22](https://github.com/compassionprojects/socialchange/actions/runs/7389850038/job/20103545544#step:8:23)7570_75060'

Another thing to note is that in rails console, using predicate title_cont results in an empty query

Story.ransack(title_cont: "Totam").result.to_sql
=> "SELECT \"stories\".* FROM \"stories\""

where as using association predicate story_updates_title_cont results in a proper sql

 Story.ransack(story_updates_title_cont: "Totam").result.to_sql
=> "SELECT \"stories\".* FROM \"stories\" LEFT OUTER JOIN \"story_updates\" ON \"story_updates\".\"discarded_at\" IS NULL AND \"story_updates\".\"story_id\" = \"stories\".\"id\" WHERE (\"story_updates\".\"title\" ->> 'en') ILIKE '%Totam%'"

which is a bit strange

Source

@madhums
Copy link
Author

madhums commented Jan 3, 2024

nvm, there's a syntax issue! 🙄

%w[title, description, outcomes, source, country]

should be

%w[title description outcomes source country]

@madhums madhums closed this as completed Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant