Jacob Montgomery

Joined

33,740 Experience
33 Lessons Completed
40 Questions Solved

Activity

Posted in Chartkick and impressionist gem render not working

As far as I can tell, to accomplish that your query is going to be quite a bit more complex.

What you end up needing is to provide a hash to chartkick that is similar to this:

[{name: "Test Raffle 2", data: [["8am-9am", 34], ["9am-10am", 12], ["10am-11am", 45], ["11am-12pm", 89]]}]

Which would give you:

In order to achieve this, you have to use the impressionists date search query. The lines below would give you the impression count for the first Raffle that occured within the last hour.

start_time = Time.now - 1.hour
end_time = Time.now
Dashboard::Raffle.first.impressionist_count( start_date: start_time, end_date: end_time)

You're going to have to make a function that queries each range of time within the date range you want, in the desired intervals (minutes, hours, days, etc) to build the array above.

Posted in Looking for advice on how to setup a relationship.

Ah, your Post model sounds like a good candidate for a polymorphic association.

Article

class Article < ApplicationRecord
  belongs_to :user
  has_many :reviews
  has_many :posts, as: :postable
end

Post

class Post < ApplicationRecord
  belongs_to :postable, polymorphic: true
  belongs_to :user
end

Then all your other models that are postable would be setup like your Article model is with has_many :posts, as: :postable

Posted in Chartkick and impressionist gem render not working

You'll want to check how the chartkick is expecting the date to be formatted.

Check: https://github.com/ankane/chartkick#data to make sure your date being passed by group_by_date matches, you may need to do some manipulation of the time before sending to chartkick.

Posted in Chartkick and impressionist gem render not working

You're going to have to do some debugging, looks like it's an error with the way the data is being presented to chartkick. You'll have to dig into how chartkick is expecting the data to be presented, then verify that your json is structured that way.

Check out this SO: https://stackoverflow.com/questions/24601985/morris-js-uncaught-typeerror-cannot-read-property-match-of-undefined

It's for MorrisJS, but net effect should be the same.

If you can throw together an example repo that produces the same results I can take a better look.

Posted in Looking for advice on how to setup a relationship.

Hey Morgan,

I don't think there's anything inherently wrong with your setup. You may play with a few query scenarios in your console to make sure you're able to access all the objects as you'd expect.

How would you setup this sort of relationship?

If I were doing this, I think I would reverse the associations of Article and Post. Since a Review and Post can't exist without an Article, I think the Article needs to be the main driving model.

class Article < ApplicationRecord
  belongs_to :user
  has_many :reviews
  has_one :post
end
class Post < ApplicationRecord
  belongs_to :user
  belongs_to :article
end
class Review < ApplicationRecord
  belongs_to :user
  belongs_to :article
end

I changed the association between Article and Post to a has_one. There's really no need for an Article to have multiple Post since a single Post object should be sufficient for you to manage the desired behavior.

You'll of course have to update your migrations:

Article

  t.string "title"
  t.text "body"
  t.string "author"

Post

  t.references "article", index: true, foreign_key: true
  t.datetime "scheduled_at"
  t.string "state"
  t.text "error"
  t.boolean "facebook_wall_post"
  t.boolean "facebook_page_post"
  t.boolean "twitter"
  t.boolean "linkedin"

Review

  t.references "article", index: true, foreign_key: true
  t.string "title"
  t.text "body"
  t.string "rating"

Posted in Chartkick and impressionist gem render not working

I think your impressions method needs to look like this

class ChartsController < ApplicationController
  def impressions
    render json: Dashboard::Raffle.all.map(&:impressionist_count)
  end
end

Check your console in your browser and see if there are any errors being thrown

Posted in Chartkick and impressionist gem render not working

Scratch that - you're using the chartkick gem. I haven't used that before, so I'll have to read up on it some, but check to make sure the chart is getting data. Does anything show up in your inspect console in your browser?

Posted in Chartkick and impressionist gem render not working

Ok great, so you can check impressionist off your list now, sounds like it's working as expected.

So for the chart, can you show your view that has your chartkick code in it?

Posted in caller

Well, here's what I came up with, althought it doesn't feel quite right but hey, all tests pass! :)

require_relative 'game'
class Game::DSL
  MOVE_DISTANCE = 10
  WIN_DISTANCE  = 100

  def initialize(game)
    @game = game
  end

  # TODO
  # Moving during a redlight will cause the game to end and lose
  def redlight!
    yield
  end

  # TODO
  # Moving during a greenlight will allow the player to move
  def greenlight!
    yield
  end

  # TODO
  # If called outside a #greenlight! or #redlight! nothing happens
  # Moves the player MOVE_DISTANCE
  # If MOVE_DISTANCE >= WIN_DISTANCE game ends and wins
  def move
    greenlight = caller_locations.map(&:label).include?("greenlight!")
    redlight   = caller_locations.map(&:label).include?("redlight!")

    if greenlight
      @game.distance += MOVE_DISTANCE
      @game.result = :won if @game.distance >= WIN_DISTANCE
      @game.finish! if @game.result == :won
    elsif redlight
      @game.result = :lost if redlight
      @game.finish!
    else
      # do nothing
    end
  end
end

Originally I had the redlight! and greenlight! methods defining a variable that I checked in the move method... then I came across caller_locations which you can check the labels, which basically equate into method names in the chain (I think).

Check: https://www.lucascaton.com.br/2016/11/04/ruby-how-to-get-the-name-of-the-calling-method

Posted in caller

Hey Roberto,

From what I can tell (and from my extremely limited knowledge in the DSL area), I believe what you'll want to do is have redlight! and greenlight! set a variable when they're called. Then in your move method, you'll check the light status to determine if it can move or not. If it attempted to move and @redlight == true, then you can set the result to lost and call finish!

Do you by chance have more of this exercise completed that you could share?

Posted in How to migrate from Heroku to Digital Ocean

Check out this page and scroll to the bottom for restore info: http://postgresguide.com/utilities/backup-restore.html

From the link:

If your database already exists you only need to run the following:

pg_restore -Fc database.bak # restore compressed binary format
pg_restore -Ft database.tar # restore tarball

However, if you're creating your database new from the restore you'll want to run a command similar to the following:

pg_restore -Fc -C database.bak # restore compressed binary format
pg_restore -Ft -C database.tar # restore tarball

Posted in Order confirmation page in rails

Hey Felender,

You need a new action in your controller that will handle the confirmation and then update your current form to GET the confirmation action instead of the create action. Then on your confirmation page, you'll replicate the form again, and this time have it POST to the create action once the user approves the transaction. Don't forget to add the route to handle the confirmation.

There's probably room for debate as to which request method to use for the confirmation action. I personally say GET just because we're not modifying any records or saving anything yet, but I'm hardly an authority on the matter :)

Check: http://www.restapitutorial.com/lessons/httpmethods.html

Posted in How to migrate from Heroku to Digital Ocean

Hey Malav,

Are you just trying to dump your old DB and import it into your new server? If so...

On your old server, dump the database:

pg_dump name_of_your_old_database > /path/to/dump/database_backup.sql

Once dumped, you'll need to transfer the *.sql file to your new server then import the database with:

psql name_of_your_new_database -f /path/to/database_backup.sql

Check: https://www.digitalocean.com/community/tutorials/how-to-backup-postgresql-databases-on-an-ubuntu-vps

Posted in caller

Hey Roberto,

Could you clarify exactly what it is you're trying to accomplish? I'm not sure what you mean by which method called which caller? What problem are you trying to solve by knowing this information?

What does your controller look like for the action? You should be using the Devise helper method current_user.

For example, don't do this:

def show
  @user = User.find(params[:id])
end

Instead, do this:

def show
  @user = current_user
end

Posted in What is wrong with my stripe configuration?

Ahh yeah, Figaro - I forgot all about that one

Glad you were able to get it sorted out - too many cooks in the kitchen is always a headache!

Posted in What is wrong with my stripe configuration?

On your Rails box where you're getting the error, if you go to the console and execute printenv, does STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY show up?

Unless I'm missing something here, when you put key/values into a yml file, that doesn't mean they're available via the ENV['foo'] method. The only way I know to convert your variables from a yml file into ENV variables is to put this into your config/application.rb:

  config.before_configuration do
    env_file = File.join(Rails.root, 'config', 'custom_yml_file.yml')
    YAML.load(File.open(env_file)).each do |key, value|
      ENV[key.to_s] = value.to_s
    end if File.exists?(env_file)
  end

There could be a gem that does this as well, so maybe that's what you're using to get your yml entries into an ENV variable?

In any case, your app isn't authenticating... so have you verified all the keys are correct and they haven't been rolled over? Try hard coding your keys in your stripe.rb and charges/new.html.erb file and see if it authenticates:

stripe.rb

Rails.configuration.stripe = {
  :publishable_key => pk_test_asdfasdfasdfasdf,
  :secret_key => sk_test_asdfadsfadsfadsf
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

charges/new.html.erb

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="pk_test_asdfasdfasdfasdf"
  data-description="A month's subscription"
  data-amount="500"
  data-locale="auto">
</script>

Posted in What is wrong with my stripe configuration?

401 errors are authorization errors, see: https://httpstatuses.com/401

Do you mean you store your keys in secrets.yml? If so then you don't read those values from the environment, instead you'd do something like Rails.application.secrets.publishable_key

So in secrets.yml

publishable_key: pk_your_publishable_key
secret_key: sk_your_secret_key

then in stripe.rb:

Rails.configuration.stripe = {
  :publishable_key => Rails.application.secrets.publishable_key,
  :secret_key => Rails.application.secrets.secret_key
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

Hi Lee,

Check out: https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes

In the first example, you'll see they made a function to return whatever they'd like to display:

  def name_with_initial
    "#{ first_name.first }. #{ last_name }"
  end

So in your case, you could do something like:

  def name_with_type
    "#{ job_name } - #{ job_type }"
  end

Then update your view like so:

  <%= f.collection_check_boxes :job_ids, @current_user.jobs.all, :id, :name_with_type  do |b| %>    
    <div class="collection-check-box">
      <%= b.check_box %>
      <%= b.label %>
    </div>
  <% end %>

I haven't tested this, so you may have to play with it some.