RJ McCollam

Joined

4,830 Experience
3 Lessons Completed
5 Questions Solved

Activity

Posted in Improve multilayer nested loops

I have built a drilldown type screen in an application I am working on and the way I set it up initially is super inefficient and causes really slow load times. The over all concept is we are displaying a break down of 4 different models that are nested underneath each other so a user can "drilldown" into what they want to see in a nice hierarchy layout.

Here is a screenshot of how it plays out.
Image of drilldown

Currently I am making this call in my controller:

@initiatives = @company.initiatives.where("start_year <= ? AND end_year >= ?", @year, @year).order(weight: :desc)

This gives me all of my model 1's in the screenshot. I then loop through those and call model 2 for each instance of model 1. And the same thing for model 2, where I loop through all of those and find each instance of model 3.

It looks like this in the view:

<% @initiatives.each do |initiative| %>

    <% initiative.annual_priorities.order(weight: :desc).each do |annual_priority| %>

        <% annual_priority.quarterly_priorities.order(quarter: :asc).each do |quarterly_priority| %>

            <% quarterly_priority.smarts.order(due_date: :asc, weight: :desc).each do |smart| %>



            <% end %>

        <% end %>

    <% end %>

<% end %>

A super expensive way to get all this information for sure, but I am not sure what other sort of structure I could use to give me the grouping that I need this data in. How might I structure this query in my controller to be more efficient?

I had to change my link to a button and set data-turbo to false in order for it to respond to what I needed it to.

I feel I am missing something fundamental, or maybe just silly, when it comes to this issue.

I am utilizing linking to anchors in most places in an app I am building and all is working well with links in the view. Formatting my path like...

sample_path(anchor: "sample_anchor")

When I attempt this via my controller via a patch method it is not taking you to the anchor when the page is loaded. Setup in my controller for the redirect_to is like...

redirect_to some_page_path(anchor: "some_anchor"), notice: "Some notice"

Posted in Auto-removing notices from a turbo_stream

I am appending a notice partial via turbo_stream on a few actions for a model. I am running into an issue on figuring out the best way to remove these notices after a set period of time.

The actual javascript on doing it is fine, but with the content being added via a turbo_stream I am not sure how to watch for that.

I have tried wrapping the notice partial with a data-controller to load a stimulus controller, but that doesn't fire when this partial is added.

I also tried listening for turbo events like load, but that isn't triggered either when this partial gets added.

What can I listen to, if anything, to run JS after a turbo_steam even has taken place?

Thanks @ugurcan, that is how I planned on calling it. Again my main issue here is getting this value into my controller so I can pass it to my turbo_stream file.

This makes sense, but in this instance I don't have a consistent model that is being output. These cards as I call them are actually each for a different model. Each card houses my create form and lists each item from the module.

So on my turbo_stream.replace I am wanting to target a manually entered ID as each one is different. I do want to send it as a param in each form, but getting it accepted into my controller is where I am getting stuck.

I have an edit user page that has multiple cards on it. I have each card wrapped in its own turbo frame tag with a unique id. On the update action in my controller if I just redirect_to @user the src attribute on the turbo frame does not update so I cannot click edit again to replace the frame.

Instead of redirecting I want to implement a format.turbo_stream. Rather than being able to replace a single partial I need to replace the one that was edited. To get this context I am including a partial name in the different form partials that is the partial name.

In creating an instance variable for that partial I was hoping I could use that in my update.turbo_stream.erb file. Like this:

<%= turbo_stream.replace "user_#{@partial}", partial: "users/display/#{@partial}" %>

In theory I think this will work, but since I am passing a param that is not part of the model it will not update my resource, and of course if I don't permit the param it gets rejected all together.

I have two questions:

  1. Am I missing a better way to update specific partials on an update action using turbo_steams?
  2. How can I get my partial parameter to come through in update action without trying to update my resource with it?

Posted in Boolean wont update

I have a column in my users table named active that is a boolean that defaults to true.

I cannot seem to update this value at all. Both in the console and through the update method in my users controller.

In my form I am using checkboxes for the boolean fields, and I have more than just the active column that I am updating. The other boolean column updates without issue.

The only sort of heads up I get is in the server logs — getting a 302 when I submit my form. This only happens when the value for active is different than true. If the value remains true I get a 200.

Feels like I am missing something super basic here.

Posted in Help parsing a string down to what I need it to be.

It does, thank you Chris. Need to spend more time working through the code to see available options like this since they aren't documented.

Appreciate the help.

Posted in Help parsing a string down to what I need it to be.

I am working on an API integration and I have to authenticate via one call, take cookie values from the response, and use them in a new call. Using HTTParty I can make all the calls I need to, but my issue comes with parsing the set-cookie response I get from my first call into what it needs to be for my second call.

Here is what I get from my first call for set-cookie:

ASP.NET_SessionId=tyq0iz2apxli30wih0xdf0jt; path=/; HttpOnly; SameSite=Lax, UserBranch=1; path=/, Locale=Culture=en-US&TimeZone=GMTM0700S; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, CompanyID=Sandbox; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, .ASPXAUTH=1869AD7D19D10655EBBD673C06E9EE641156F95DAA9ADFCD938F3836F916AAEA90CD8FCE23783C55E82F6E66C1A9DAD4B743B33AC033F52954D04526B050C7C77AEF6209A05238B2C79B61391F0E5DC14C7EC4CEFDBDD15DE4234E2DF2ED20B09011C817A9D61E3CFDB1E2787B6C9BDA825173FF2B7BCB7D35AC67615412BD13547DA958CFF05F62AADB94BE480B0E6A1A966F36; path=/; HttpOnly, ASP.NET_SessionId=tyq0iz2apxli30wih0xdf0jt; path=/; HttpOnly; SameSite=Lax, UserBranch=1; path=/, Locale=Culture=en-US&TimeZone=GMTM0700S; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, CompanyID=Sandbox; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, .ASPXAUTH=1869AD7D19D10655EBBD673C06E9EE641156F95DAA9ADFCD938F3836F916AAEA90CD8FCE23783C55E82F6E66C1A9DAD4B743B33AC033F52954D04526B050C7C77AEF6209A05238B2C79B61391F0E5DC14C7EC4CEFDBDD15DE4234E2DF2ED20B09011C817A9D61E3CFDB1E2787B6C9BDA825173FF2B7BCB7D35AC67615412BD13547DA958CFF05F62AADB94BE480B0E6A1A966F36; path=/; HttpOnly, requestid=693977198F02788111EBA11C82CB901C; path=/, requeststat=+st:92+sc:~/entity/auth/login+start:637544397471740106+tg:; path=/

Here is what I need it to be in order to successfully make my second call:

.ASPXAUTH=1869AD7D19D10655EBBD673C06E9EE641156F95DAA9ADFCD938F3836F916AAEA90CD8FCE23783C55E82F6E66C1A9DAD4B743B33AC033F52954D04526B050C7C77AEF6209A05238B2C79B61391F0E5DC14C7EC4CEFDBDD15DE4234E2DF2ED20B09011C817A9D61E3CFDB1E2787B6C9BDA825173FF2B7BCB7D35AC67615412BD13547DA958CFF05F62AADB94BE480B0E6A1A966F36; ASP.NET_SessionId=tyq0iz2apxli30wih0xdf0jt; CompanyID=Sandbox; Locale=Culture=en-US&TimeZone=GMTM0700S; UserBranch=1; requestid=693977198F02788111EBA11C82CB901C; requeststat=+st:92+sc:~/entity/auth/login+start:637544397471740106+tg:;

I have two questions:

  1. What is an efficient way to remove the items I do not need. Just multiple gsubs?
  2. What can I do about duplicate values in the response I get from the first call? For instance I get multiple .ASPXAUTH entities that are duplicates of each other. How do I only strip one of them out?

Of course everything works nice and neat in Postman, but trying to implement it I am running into these issues.

As I make progress and think through this I guess I can't rely on referencing this value in an initializer unless I want to restart the app every day. Which I don't.

The better solution seems to wrap my API calls in my own wrapper rather than relying on the gem I was going to use.

I am working on an application that integrates with Keap (a CRM). The integration with Keap is at the application level in that it only connects to one Keap account, and isn't a connection per user in the application.

The Keap API authenticates with OAuth and I have 2 questions I am looking for some guidance on

  1. Storing my access/refresh tokens — I'm not entirely sure the best place to store these values. I will be referencing their values in an initializer and will be updating them on the regular. Is there a better mechanism to store these values outside of creating a new model with these fields and always just updating the first record in that model?
  2. Recurring/CRON Jobs - I will need to run a job every 23hrs to refresh my access token. Assuming I go the model route on the question above my plan would be to setup an active job to be setup every time a record is updated. Setting the wait to be 23hrs from when the update happens. Is there a more efficient way to do this, or is ActiveJob the rails way to accomplish this?

Posted in Stimulus not autoloading controllers

Turns out this is an issue in Safari, but not in Chrome and Firefox.

Seems like kind of a huge issue and so anti-pattern to the basecamp/DHH way of doing things. Maybe I am missing something though.

Posted in Stimulus not autoloading controllers

Decided to give stimulus a try on a new app I am working on and immediately ran into issues. Up to this point this is a fresh rails 6.1 app and I have installed Hotwire-rails.

Just using the hello example controller that comes by default I get this error in the console - Failed to autoload controller: hello.

What is weird to me is that if I remove the targets array and simply have

connect() {
    console.log('hi')
}

I will get that output in the console. So Stimulus is "working" in a sense, but not the way it should be.

Posted in Format of created_at for date range using Searchkick

What I had before that was causing it to be put in an array was

<%= date_field :start_date, nil, value: params[:start_date] %>

and what I changed it to get just a normal string was

<%= date_field nil, :start_date, value: params[:start_date] %>

Posted in Format of created_at for date range using Searchkick

This whole thread goes to show my greeness in rails. The way that I was setting things up in my controller was fine. The issue was the way I had my date_field setup in my view. It was giving me the value in an array which was the cause of the error. Corrected my field setup to return just plain text and all is well with the world.

In all seriousness Chris you need to put a tip button on this site cause the attention you gave, especially considering the holiday, is more than appreciated. Yes I pay monthly for access, but this got me over a hump to meat a deadline. Thank you.

Posted in Format of created_at for date range using Searchkick

I think the issue might be in the format that the params are being sent through as.

If I set up simple variables for params like

client = params[:client]
start_date = params[:start_date]
end_date = params[:end_date]

then the output I get is

>>  client
=> "2"
>>  start_date
=> ["2018-11-01"]
>>  end_date
=> ["2018-11-30"]

So the dates are being wrapped as an array for some reason. Which is proably why that extra ] is what you were thinking could be the problem. Guess I need to fix that before I can go any further.

Posted in Format of created_at for date range using Searchkick

When I get the error and output the args I get what I think I am suppose to be getting {:client=>"2", :created_at=>{:gte=>["2018-11-01"], :lte=>["2018-11-30"]}}

Posted in Format of created_at for date range using Searchkick

In this particular instance to just test if it is working I am always making sure that those fields have values in them.

Posted in Format of created_at for date range using Searchkick

Not sure if the last comment came through or not. Not seeing it on my end, but post count for this thread went up.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.