Chris Oliver

Joined

290,710 Experience
86 Lessons Completed
298 Questions Solved

Activity

Posted in Upgrade to Mojave on Mac OS

Hey Pam!

I think a few things will break, but you should be fine. I don't have an upgrade guide specifically, but the steps are about the same:

  1. Upgrade your OS
  2. Upgrade & Install the latest XCode: xcode-select --install 2.5. You may also need to install XCode's Command Line Tools
  3. Upgrade your Homebrew install
  4. If you get an ld: library not found for -lstdc++ error installing gems, you can run: sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

I think that should be it.

Posted in Where do this code go? (Refactoring Rails Workers)

Ah yes, sorry! :)

I would pull the logic out into its own class. You can either make it a base class for the job, or you can make it a dedicated class.

This example is the inheritance approach:

class BaseWorker
  def do_this_action(website)
    ...
  end
end

class WebsiteWorker < BaseWorker

  def perform(website)
    do_this_action(website)
  end
end

Or if you wanted a dedicated class:

class DoThisProcessor
  def perform(website)
  end
end

class WebsiteWorker
  def perform(website)
    DoThisProcessor.new.perform(website)
  end
end

They're both very simple so you won't have too many tradeoffs either way you go in this case.

Posted in Where do this code go? (Refactoring Rails Workers)

Is it running the job twice? There's nothing here that would duplicate the calling of do_this_action that I can see.

Posted in Improve on these ActiveRecord requests?

Yeah, just adding a column like visits_last_30_days:integer and having it update every hour or night. You'd just query the impressions, sum up the total and save it to that column each time.

If you had a few common options you could cache multiple values so those common ones were super fast. Then fallback to doing the JOIN query for custom ranges which will be a little slower.

You should be able to query the Product table and join the impressions and use that for sorting. Here's an example of doing that: https://stackoverflow.com/questions/22234983/rails-activerecord-sort-by-count-of-join-table-associations

Skipping the cache will help you keep things simpler, since you'll always need the fallback join query.

Posted in Improve on these ActiveRecord requests?

You might try adding your own caches to the Product table. You could have a nightly or hourly cron update the cache so it isn't constantly being changed, but still getting regular updates.

I would recommend Rails's ActiveStorage. I've done some episodes on it which you can checkout, but it's the official Rails file uploading library and definitely the easiest.

You'll still need to have imagemagick installed. They all use this for the actual cropping and resizing of images.

Carrierwave works too, but it requires some other setup like creating Uploaders so you can define how a file gets processed.

ActiveStorage works a bit different where you define how the file gets resized when you use it in your view. Carrierwave does this as soon as the file is uploaded instead. Both have some advantages but ActiveStorage should definitely be easier.

Posted in How do I create my homepage subdomain ('www')

You can pass subdomain: nil into your url helpers to change the subdomain. Something like link_to home_url(subdomain: nil)

Posted in Rails Application Templates Discussion

Hey Mark, I just fixed that. Just need to modify the navbar link to use "#" instead of root_path. https://github.com/excid3/jumpstart/commit/7a49c0fe5cbbd3621288cb00b9ca390099d869a9

Dropdown is fixed now. Just change the root_path in the link_to for the dropdown navbar.html.erb to "#".

All the navigation is in app/views/shared/navbar.html.erb 👍

Posted in How do I create my homepage subdomain ('www')

Hey Damian!

It's usually easiest to set that up in your routes file. You can setup a subdomain constraint so that when the subdomain is www or nil, you can render the homepage. Otherwise when there's a subdomain, you can send it to the account controller.

You can put this in app/lib/account_subdomain.rb

class AccountSubdomain
    def self.matches? request
          # www and no subdomain are excluded and don't count as account subdomains
            # You can add any more subdomains to this array to have them ignored.
          !["www", nil].include? request.subdomain
    end   
end

Then your config/routes.rb file can use this to set the constraints:

Rails.application.routes.draw do
    # Account routes
    constraints(AccountSubdomain) do
            root to: "account#index"
    end

        root to: "home#index"
end

It's your standard bootstrap dropdown, so double check and make sure there are no JS errors in the browser console.

I'm not at the computer right now, but I can take a look tomorrow hopefully.

Great! Let us know how it goes. I'm online a bit more during the week (and after the holidays) so sorry for the late replies.

My personal advice is generally to build it from scratch so you can understand how it works in-depth. And if you realize it'll be too much work, then using the apartment gem can help save you some time.

Hey Damian,

Like the error says, looks like you'll need to run "rails webpacker:install"

/home/meaw/.rvm/gems/ruby-2.5.1/gems/webpacker-3.5.5/lib/webpacker/configuration.rb:79:in `rescue in load': Webpacker configuration file not found /home/meaw/work/weepx/two/weepx/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /home/meaw/work/weepx/two/weepx/config/webpacker.yml (RuntimeError)

Jumpstart does that for you automatically, so you shouldn't have to, but maybe something failed when you created it originally.

Sounds like something is killing the web process. What happens if you just run rails s instead of foreman?

Hey Damin,

You can handle subdomains from scratch like so: https://gorails.com/episodes/subdomains-and-multi-tenancy-from-scratch?autoplay=1

An alternative is to let the Apartment gem do it for you. Covered that here, they have a subdomain option as well. https://gorails.com/episodes/multitenancy-with-apartment?autoplay=1

You'll have to set your DNS to redirect *.website.com to your server for this to work.

Yep! You can do it with a Rails application template like the one I built called Jumpstart: https://github.com/excid3/jumpstart

This episode talks about how to build your own: https://gorails.com/episodes/rails-application-templates

You'd just copy over all your code into a template and use it every time you create a new app.

When you <%= render comment.comments, it isn't specifying any order, so you'd want to sort that by created_at if you want them in a particular order. A SQL query doesn't respect any ordering unless you tell it to.

<%= render comment.comments.order(created_at: :asc)

Posted in How long does it take for comments to post?

The episode comments are also forum threads, so let me know if you still have issues. I need to go through the forum code some more and polish it up again. It got overrun a bit by spammers before so I've been trying to get it back in order.