ruby on rails - Search by empty list for many-to-many relation using ransack -
i searched how pass empty array ransack, example:
@search = promotionsretailer.search(retailer_id_in: [])
this sql statement:
"select `promotions_retailers`.* `promotions_retailers` "
i found link, add -1 empty array, used search(retailer_id_in: ([] + [-1]))
.
- any solution better solution?
- how search using
retailer_id
in promotion table, if have many-to-many relation between promotions/retailers without using breaking tablepromotionsretailer
ransack gem?
first part of question vital. should separate question.
unfortunately, there in no more elegant way cope it, than:
search(retailer_id_in: ([] + [-1]))
the problem lays somewhere in gem. , if pass empty array, or array filled nil
values parameter, there join in sql, conditions ignored, e.g.:
ruby:
article.ransack({authors_id_in: [nil, nil, nil]}).result
sql:
select "articles".* "articles" left outer join "articles_authors" on "articles_authors"."article_id" = "articles"."id" left outer join "authors" on "authors"."id" = "articles_authors"."author_id"
your second part of question searching through habtm relations using ransack, explained in rails 3.1 ransack habtm pretty good.
therefore:
promotion.ransack({retailer_id_in: [1,2,3]}).result
you should not use promotionsretailer
class itself.
Comments
Post a Comment