Chris Oliver

Joined

292,890 Experience
93 Lessons Completed
295 Questions Solved

Activity

At a high level, you'd have to:

  1. Create a new database for every account in your system. Accounts probably live in the primary database, but then you'll have separate databases for their data.
  2. Migrate the data
  3. Have a before_action on ApplicationController that switches the ActiveRecord::Base connection based upon the account.

ActiveRecord::Base.establish_connection({:adapter => "postgresql", :database => current_account.db_name, :host => "wherever", :username => "username", :password => "password" })

Rails 6 is adding some support for multiple databases, but I'm not sure if it'll be useful for what you're trying to accomplish.

Also keep in mind (via https://stackoverflow.com/a/1157008/277994)

A PostgreSQL "schema" is roughly the same as a MySQL "database". Having many databases on a PostgreSQL installation can get problematic; having many schemas will work with no trouble. So you definitely want to go with one database and multiple schemas within that database.

Apartment uses schemas like this by default, so you may just want to use that.

Posted in i'd like to replace ransack with elasticsearch.

All my elasticsearch episodes are using the Searchkick gem which has been great for me.

You'll also need the ElasticSearch server running locally for development and then with the Heroku addon in production.

Posted in Format of created_at for date range using Searchkick

Those plain fields that aren't using form like form.date_filed always trip me up too. Small argument differences can mess up your params really easily. Thanks for sharing! :D

Posted in Format of created_at for date range using Searchkick

Ah ha! That was a weird one to track down! Glad you got it fixed.

What did you do to fix your date field? I imagine someone might find your solution helpful as well in case they stumble upon this.

A tip button is a fun idea. Paypal's got something kinda like that: https://www.paypal.me/gorails

Posted in Format of created_at for date range using Searchkick

Also I found this when searching the error:

https://stackoverflow.com/questions/48749364/how-to-fix-the-error-failed-to-parse-date-field-in-elasticsearch

Which points out to me that you may be passing in data wrong. It appears to put the value it cannot parse inside the [] here...and that makes it seem like your value is just a ].

failed to parse date field []]

Have you double checked to make sure that args is printing out everything correctly? I still feel like that's the source of your problem.

Posted in Format of created_at for date range using Searchkick

Okay yeah. :)

I wonder if you need to specify range before the attribute like it shows in the docs. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

What happens if you add debug: true to your query?

Product.search("soap", debug: true)

Posted in ActiveStorage trix image before record exist.

Forgot to mention, this is what Basecamp does when you create like a new Message. Same concept as writing a draft email in GMail too.

Adding this to my list for a screencast. 😜

Posted in Format of created_at for date range using Searchkick

It probably got flagged as spam cuz it was huge.

Important part of the error was this:

[400] {"error":{"root_cause":[{"type":"parse_exception","reason":"failed to parse date field []] with format [strict_date_optional_time||epoch_millis]"},

It looks like one of the date fields might not have been passed through.

Have you logged out your args var before it gets sent to Searchkick? I'm curious to see what that looks like. Usually you only want to include the field if it was present, and I don't see checks on that in your code for the date range fields. Maybe that is the issue.

Posted in ActiveStorage trix image before record exist.

I would probably recommend the middle option. One improvement you can make on that rather than deleting if the user cancels is to just have an idea of a "draft" record.

This would be a record you're editing but haven't fully published yet. It would only be visible to you and that way your images are attached to this record and you can safely delete them.

That helps in a lot of ways and also makes it so the user doesn't lose any progress as well which can be nice.

Posted in Format of created_at for date range using Searchkick

Can you post the error? I'm pretty sure it tells you which portion of the params was invalid.

It's been a while since I've done this, but I remember having to specify "range" or something.

Looks like you can do that: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html

Not sure if you can do it with Searchkick directly, so you'd have to ask them.

Yo, @kwiksource, I just updated the source code in the course. You can redownload it from the course to get the latest copy. 👍

Hey man! I'm gonna update this real quick. Working on a new version of the course as well that will come out in the next couple weeks.

I'll let you know when the code is updated. 👍

Posted in just ran into some strange validation problem.

Did you mean greater_than_or_equal_to: 0 ?

The code right now wouldn't allow the quantity to ever be 0, but that seems like something you'd want to support.

Posted in What is wrong in my script?

Your new_response action sets up answers in memory, but your create_response action is the one to save it.

However you're rendering create_response.html.haml where the error came from. Seems like you're looking at the wrong view.

Possibly fixed by doing the following:

def create_response
  @survey = Survey.find(params[:id])
  @response = @survey.build(response_params)
  @response.user = current_user
  if @response.save
      redirect_to @response
    else
      render action: :new_response
    end
end

That'll redirect to the response if it saves successfully and mimics the normal create action.

Posted in Nested Comment Threads in Rails - Part 1 Discussion

Hey Lunik,

It sounds like SimpleMDE is not getting attached to the hidden reply comment forms.

For GoRails, I had to do this, so it applies SimpleMDE to any visible forms and then it also applied it when you click the Reply link.

Apologizes for the messy code, this could use some refactoring. :)

simplemde = []

cleanupSimpleMDE = ->
  # Clean up if already exists
  if simplemde.length > 0
    for editor in simplemde
      editor.toTextArea()
    simplemde = []

addSimpleMDE = (field) ->
  editor = new SimpleMDE({
    autoDownloadFontAwesome: false,
    element: field,
    toolbar: ["bold", "italic", "heading", "|", "code", "quote", "unordered-list", "ordered-list", "clean-block", "|", "link", "image", "|", "preview", "side-by-side", "fullscreen", "guide"],
    spellChecker: false
    status: false
  })

  simplemde.push(editor)
  return editor

@setupEditors = ->
  for editor in $(".simplemde:visible")
    addSimpleMDE(editor)

  $("[data-behavior='show-comment-form']").on "click", (e) ->
    e.preventDefault()
    reply_link = $(this)
    reply_link.hide()

    form = reply_link.parents(".forum_post").first().find("[data-behavior='comment-form']")
    form.show()
    addSimpleMDE(form.find("textarea")[0])

$(window).on 'popstate', cleanupSimpleMDE
$(document).on 'turbolinks:before-visit', cleanupSimpleMDE

$(document).on 'turbolinks:load', @setupEditors

Posted in Setup MacOS 10.14 Mojave Discussion

I haven't experienced it myself, but looks like there's some info on StackOverflow https://stackoverflow.com/questions/51270680/brew-wont-upgrade-on-mojave

Posted in Anyone had experience with Wasabi Cloud Storage?

Sure promises a lot for very cheap! I'd be curious to hear about how that experience goes.

If it's anything like DigitalOcean Spaces where it's API compatible with S3, then you can just change the host that they're going to.

I covered that a couple times in the past with DO Spaces:

https://gorails.com/episodes/digital-ocean-spaces-with-rails
https://gorails.com/series/direct-uploads-to-amazon-s3

You should then just be able to change the domain / host to point to Wasabi like we do here for Spaces, set the CORS config (if you're doing direct uploads, not needed if you're uploading to Rails and having Rails upload to storage) and be set.

Sweet, thanks for sharing that! Makes a lot of sense to use any built-in functionality that you can with ElasticSearch. This is a good improvement.

Posted in Search Filters/Report Generator

You can search multiple models at once with Searchkick by doing the following:

Searchkick.search "milk", index_name: [Product, Category]

And you can also customize the search_data method on the model to include attributes from associated models.

This example builds up a string to index of all the tags stored in an association on the record.

  def search_data
    {
      name_tagged: "#{name} #{tags.map(&:name).join(" ")}"
    }
  end