This is tangentially related to Avo but
# avo-2
a
This is tangentially related to Avo, but I noticed that Ransack 4 requires a
ransackable_attributes
method, and if it's missing, the search query will fail silently. I don't know whether this is just something that should be in the documentation, or if Avo can catch it and provide a warning, but I know that Avo 3 requires Ransack, so it's a reasonable heads-up.
l
does it fail silently?
oh. it makes the search not return anything
yeah. ok. that's reasonable
I'll make a GH issue for it to track it
a
well, it makes Global search completely break.
i.e. if you have one broken search, global search completely breaks.
l
yes. makes sense
a
ransack has been driving me completely nuts anyway.
i can't seem to get Ransack 4 "simple mode" to work at all.
l
I haven't tried the simple mode
oh, with the form and everything?
yeah, it doesn't look that simple 😛
a
does Avo patch ransack at all?
l
the params structure is quite complex to understand and it's not properly documented. I had to go into the source code to figure everything out for
avo-advanced_filters
a
I just mean something like
User.ransack(params[:q]).result
should just work, but it does not.
l
no. we don't patch anything we use
we try to keep things simple and not patch things that might break for everyone
a
for sure
so if i have
Copy code
> User.ransackable_attributes
=> ["username", "first_name", "last_name", "phone", "is_employer", "uuid"]
i get
Copy code
> User.ransack("jcn").result.to_sql
=> "SELECT \"users_user\".* FROM \"users_user\""
but then i try
Copy code
> User.ransack(username_cont: "jcn").result.to_sql
=> "SELECT \"users_user\".* FROM \"users_user\" WHERE \"users_user\".\"username\" ILIKE '%jcn%'"
i would have expected
.ransack
to search against all ransackable_attributes
l
lol
"simple mode"
l
I don't think that works that way
a
or is
q
a giant data structure?
l
I haven't checked the source code, but I know that
ransackable_attributes
were an afterthought added after the security issues surfaced a while back
I don't knwo if it's that smart
but I see your point. makes sense!
a
ok, so when you pass something in from global or resource search, are you just passing a string and then leaving it to the user to come up with the ransack query themselves?
l
yup
you are responsible for the query and we recommend you add the attributes there
it's a bit more work, and might seem a bit repetitive kowing you declared tehm in
rans_attributes
, but you are more sure on what's happening there instead of relying on what ransack it's doing in the background
a
yeah, that's what we're doing now, it's just ugly.
Copy code
query.ransack(company_name_cont: params[:q], user_first_name_cont: params[:q], user_last_name_cont: params[:q], user_phone_cont: params[:q], user_username_cont: params[:q], requester_first_name_cont: params[:q], requester_last_name_cont: params[:q], requester_phone_cont: params[:q], requester_username_cont: params[:q], m: "or").result(distinct: false)
(this is searching across 2 associations)
l
I know. you could create a helper functintion that takes all the
rans_attrs
and
rans_assoc
, adds the prefix and spits out those arguments
query.ransack(compile_ransack_attributes_for(User)).result(distinct: true)
a
yeah. i'll just deal with it for now.
l
and
compile_ransack_attributes_for
goes into that model and created those
that's a cool thing to add to Avo TBH 😛
I'll create a ticket
a
glad i could inspire 🙂
l
yeah. thanks!