RJ McCollam


4,810 Experience
3 Lessons Completed
5 Questions Solved


Posted in Format of created_at for date range using Searchkick

I am using searchkick to build out a searchform that has a couple different parts to it and searches across multiple models. I am specifically trying to get a date range setup so the user can search for results within a start and end date.

Here is how I am building my search query in my controller:

def index
        @page_title = "Search"

        query = params[:q] ? params[:q] : "*"
        args = {}
        args[:client] = params[:client] if params[:client].present?
        args[:created_at] = {}
        args[:created_at][:gte] = "2018-10-01"
        args[:created_at][:lte] = "2018-11-30"
        @results = Searchkick.search(query, where: args, order: {created_at: :desc}, aggs: { client: {} }, operator: "or")

In passing in a hard coded value for a start and end date it does exactly what I want it to do.

When I attempt to pass these values in from the form in the same format I get a parse error.

I did go through and set the search_data up for each model in order to convert created_at to a time using created_at: created_at.to_time, but in indexing that data I ran into issues where my other search functions weren't working since my models don't all share the same data.

Am I missing the boat with my current implementation completely, or is there some formatting I can do in order to get this date range working?

I know I am reaching a little bit with my questions, but if all models DID have the same attributes, would I be able to setup aggregations just like in the video?

Is it possible to still use Aggregations when searching multiple models? Especially if each model doesn't have that particular value?

Posted in ElasticSearch connection refused

In my instance the issue was with localhost not being represented in my hosts file.

I adeed the following to it and was able to get past this.       localhost broadcasthost
::1             localhost
fe80::1%lo0     localhost

Posted in Search Filters/Report Generator

Thanks Joe and Chris for both of your feedback. I decided to go with ElasticSearch and Searchkick since there seems to be more current documentation for what I am trying to accomplish.

Posted in ElasticSearch connection refused

Mason, I know this was over a year ago you posted about it, but just curious if you ever fixed this. Or more importantly if you remember how you fixed it.

Running into the same issue on my end.

Posted in Search Filters/Report Generator

I am working on a project for a client that consists of a primary jobs model that has a number of forms (other models) within it.

I am needing to build out search functionality that searches through all models in order to return a job that matches. This search also needs to include filters with the ability to filter by client, time frame, and a couple other pieces of data.

In looking through possible solutions i think that the most recent video Chris did on elasticsearch with filters (https://gorails.com/episodes/search-filters-with-elasticsearch-aggregations) might be what I am after.

What I havent seen in the research I have done so far is searching through multiple models. Would elasticsearch with filters be a good option to accomplish what I am trying to?

Posted in Nested Model of an already nested model

I want to make sure my thinking is right more than anything else on this post. Hopefully I can explain myself clearly and get pointed in a good direction.

My Scenario

I have 3 models. Model A, Model B, and Model C.

Model A is the top level model and has Model B nested underneath it through a belongs_to/has_many setup.

I am needing to add a nested model (Model C) to Model B. Model C has not dependcy on Model A. Only Model B.

In building out Model C can I just go about it as I normally would using a belongs_to/has_many setup?

I imagine that going another level deep with nesting is possible, but I also imagine there are some special things to look out for in my routing and form setup.

My question in all of this being can I do it and what should I watch out for in doing so?

Posted in Form errors not displaying

Ah, thank you Chris. Updated that and I am all set. Really appreciate you answering.

Posted in Form errors not displaying

In my model for users I am validating the presence of a number of attributes using:
validates_presence_of :parent_1_first_name, :parent_1_last_name...

The beginning of my form object looks like this:
<%= form_for @user, url: {action: 'update'} do |f| %>

            <% if @user.errors.any? %>

                <div class="form-errors">

                    <h2><%= pluralize(@user.errors.count, "error") %> stopped you from updating this user:</h2>
                    <% @user.errors.full_messages.each do |msg| %>
                      <h4><%= msg %></h4>
                    <% end %>


            <% end %>

My Update method in my User controller looks like this:
def update
        subaction = params[:subaction]
        if @user.update(edit_user_params)
       flash[:success] = "#{@user.first_name} #{@user.last_name} has been updated."
       redirect_to edit_user_path(@user, anchor: subaction)
      flash[:error] = "Something went wrong and #{@user.first_name} #{@user.last_name} could not be updated. Please ensure all required fields have been filled out."
        redirect_to edit_user_path(@user)

When a user is updated, but is missing one of the fields I am checking form error messages do not display and instead I get a redirect to the edit path with my error message instead.

In googling around I don't see what I have setup incorrectly.

Posted in Implementing global app settings

Thanks Andrew. Wanted to make sure my head was in the right place and I wan't missing something obvious.

Posted in Implementing global app settings

I am wondering what the best way to go about setting up Settings for an application. Settings will consist of name, address, phone number, and some other project specific entries. What has me a little confused is that while many users will be accessing the app, these settings are to be specific to the app and not the user.

I will be letting admins edit these settings and my thought was to setup a model for Settings to store the values in the DB. However there wont be multiple Settings entires in the DB I just need one.

Is a model the right way to go in this instance and maybe I just manually create one entry for Settings that can be edited by anyone with the correct role?

Seems like a simple concept to me, but getting a little mixed up by it.

I have a user model in my application that has instances of 3 other models and what I am trying to do is to export to CSV all of the user information as well as the information for the other models related to that user.

In following the Export to CSV video on GoRails I am able to export all of the user's data through CSV, but am not sure how to also include records from these other models.

Since each user will have different totals of these other models I understand I probably cannot put all the data of a user on one line and would have to break it down like:

User Data
Parent Data for Above User
Medication Data for Above User

I'm not sure how to loop through these other models related to the user and output there data in my to_csv function.

Here is the code I am using to generate the CSV currently:

CSV.generate(headers: true) do |csv|
  csv << attributes
      all.each do |user|
          csv << user.attributes.values_at(*attributes)

Is this even possible considering that the headers would be different? Should I just make them separate exports?

Edit - Maybe a better question is if I break these out into separate CSV's I would only have the user_id in order to allow any association between users and the data in other CSV's.

How would I go about pulling in a value from the User model using the user_id in a CSV export for another model? For example in my parents export I wont to include the user's student_id.

I had the config.resource_keys uncommented for some reason and once I commented it out all worked as expected.

Thanks for your help here Chris.

Yes I did restart rails a few times to make sure it wasn't something silly like that.

The full trace can be found here - http://www.awesomescreenshot.com/image/2486026/389892e4c6723ca10f0a40718c89bdba

I have followed the instructions on this page https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-with-something-other-than-their-email-address to allow signing in with something other than the email, but when I try to login I get an error of unidentified method for the new key I am using. In this case student_id. I am able to create an account with the student id and email just fine.

In application_controller.rb I have added the parameters for both sign up and sign in.

  before_action :configure_permitted_parameters, if: :devise_controller?


    def configure_permitted_parameters
        devise_parameter_sanitizer.permit(:sign_up, keys: [:student_id, :email, :password, :password_confirmation])
        devise_parameter_sanitizer.permit(:sign_in, keys: [:student_id, :password])

In the devise initializer I have added the link config.authentication_keys = [:student_id]

Not sure what else I am missing, and don't know enough to read into the error all that well.

Posted in Ruby version stuck on macOS

I got this figured out. It looks like there was a rouge line in my .bash_profile that was putting rbenv in my $PATH and was casuing issues.

Cleared that up and was able to get things up and running.

Posted in Ruby version stuck on macOS

Haven't figured it out just yet.

In looking at .ruby-version and Gemfile they both have the correct version of Ruby I am wanting to use. Seems bizarre to me.

Posted in Ruby version stuck on macOS

Setting up my new MacBook Pro and am running into an issue with ruby versions.

At this point I have installed and am usin rbenv. I installed ruby 2.3.1 and when I ruby -v that is what it shows.

When I try to start the server on an app that specifies ruby 2.3.1 in the gemfile and in .ruby-version I get this message - "Your Ruby version is 2.0.0, but your Gemfile specified 2.3.1".

Not sure where this rouge setting of 2.0.0 is at.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2023 GoRails, LLC. All rights reserved.