Search code examples
ruby-on-railsrubyherokuheroku-postgres

Is there a way to rewrite Sqlite3 RoR controller syntax for Heroku PostgresSql? I'm getting a syntax error in my controller


I have a RoR backend for a note taking application. I have a custom parameter that I send to my Rails API along with the database object itself. The backend works fine locally, and I have access to all of the custom params that I need in the API. But when I switch to production mode for Heroku -which implies switching from Sqlite3 to PostgresSql - I get a syntax error in my controller.

This is the way that works fine locally:

  def show
    @question = Question.find(params[:id])
    star_count = @question.starring_users.length
    render( {json: @question, include: [:comments ,:collections, :starring_users], star_count, status: :ok} )
  end

And this is the error I got when pushing to Heroku:

/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require': /app/app/controllers/questions_controller.rb:10: syntax error, unexpected ',', expecting => (SyntaxError)
..., :starring_users], star_count, status: :ok} )
...                              ^
/app/app/controllers/questions_controller.rb:51: syntax error, unexpected end-of-input, expecting end

This is my Gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use sqlite3 as the database for Active Record
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'


group :test, :production do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'pg'


end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem "jwt", "~> 2.1"

and my Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.2.3)
      actionpack (= 5.2.3)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailer (5.2.3)
      actionpack (= 5.2.3)
      actionview (= 5.2.3)
      activejob (= 5.2.3)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (5.2.3)
      actionview (= 5.2.3)
      activesupport (= 5.2.3)
      rack (~> 2.0)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (5.2.3)
      activesupport (= 5.2.3)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.3)
    activejob (5.2.3)
      activesupport (= 5.2.3)
      globalid (>= 0.3.6)
    activemodel (5.2.3)
      activesupport (= 5.2.3)
    activerecord (5.2.3)
      activemodel (= 5.2.3)
      activesupport (= 5.2.3)
      arel (>= 9.0)
    activestorage (5.2.3)
      actionpack (= 5.2.3)
      activerecord (= 5.2.3)
      marcel (~> 0.3.1)
    activesupport (5.2.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    arel (9.0.0)
    bcrypt (3.1.12)
    bootsnap (1.4.2)
      msgpack (~> 1.0)
    builder (3.2.3)
    byebug (11.0.1)
    concurrent-ruby (1.1.5)
    crass (1.0.4)
    erubi (1.8.0)
    ffi (1.10.0)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    i18n (1.6.0)
      concurrent-ruby (~> 1.0)
    jwt (2.1.0)
    listen (3.1.5)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
      ruby_dep (~> 1.2)
    loofah (2.2.3)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (0.9.2)
    mimemagic (0.3.3)
    mini_mime (1.0.1)
    mini_portile2 (2.4.0)
    minitest (5.11.3)
    msgpack (1.2.9)
    nio4r (2.3.1)
    nokogiri (1.10.2)
      mini_portile2 (~> 2.4.0)
    pg (1.1.4)
    puma (3.12.1)
    rack (2.0.6)
    rack-cors (1.0.3)
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (5.2.3)
      actioncable (= 5.2.3)
      actionmailer (= 5.2.3)
      actionpack (= 5.2.3)
      actionview (= 5.2.3)
      activejob (= 5.2.3)
      activemodel (= 5.2.3)
      activerecord (= 5.2.3)
      activestorage (= 5.2.3)
      activesupport (= 5.2.3)
      bundler (>= 1.3.0)
      railties (= 5.2.3)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.0.4)
      loofah (~> 2.2, >= 2.2.2)
    railties (5.2.3)
      actionpack (= 5.2.3)
      activesupport (= 5.2.3)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.19.0, < 2.0)
    rake (12.3.2)
    rb-fsevent (0.10.3)
    rb-inotify (0.10.0)
      ffi (~> 1.0)
    ruby_dep (1.5.0)
    spring (2.0.2)
      activesupport (>= 4.2)
    spring-watcher-listen (2.0.1)
      listen (>= 2.7, < 4.0)
      spring (>= 1.2, < 3.0)
    sprockets (3.7.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    sqlite3 (1.4.0)
    thor (0.20.3)
    thread_safe (0.3.6)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    websocket-driver (0.7.0)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.3)

PLATFORMS
  ruby

DEPENDENCIES
  bcrypt (~> 3.1.7)
  bootsnap (>= 1.1.0)
  byebug
  jwt (~> 2.1)
  listen (>= 3.0.5, < 3.2)
  pg
  puma (~> 3.11)
  rack-cors
  rails (~> 5.2.3)
  spring
  spring-watcher-listen (~> 2.0.0)
  sqlite3
  tzinfo-data

RUBY VERSION
   ruby 2.6.1p33

BUNDLED WITH
   2.0.1

I've tried a few different rewrites of the line to no avail. If you have any suggestions I would appreciate hearing them.


Solution

  • That will not work fine locally. This has got nothing to do with Sqlite vs Postgres. That is simply a ruby syntax error.

    Minimal reproduction example:

    {json: @question, star_count}
    

    You cannot declare a standalone variable like that in the middle of a key-value Hash. Each item in the Hash needs to have a key and a value.

    Perhaps what you meant to do was something like this:

    render({
      json: @question,
      include: [:comments, :collections, :starring_users],
      star_count: star_count,
      status: :ok
    })
    

    ...But whatever it is you're trying to do, the point is that this is purely a syntax error. Nothing to do with the database engine, and I guarantee that the code does not work locally either.