1

The migration:

class CreateTableSalonWebProfiles < ActiveRecord::Migration
  def change
    create_table :salon_web_profiles do |t|
      t.references :salon, null: false
      t.boolean :attendance_domicile, default: false
      t.boolean :parking_lot, default: false
      t.boolean :wifi, default: false
      t.boolean :physically_impaired_accessibility, default: false
      t.boolean :snack_bar, default: false
      t.boolean :parking_meter, default: false
      t.timestamps
    end
    add_index :salon_web_profiles, :salon_id, unique: true
    add_foreign_key :salon_web_profiles, :salons
  end
end

The Salon model:

class Salon < ActiveRecord::Base
...
  has_one :salon_web_profile, dependent: :destroy
...
end

The new model:

class SalonWebProfile < ActiveRecord::Base
  belongs_to :salon

  validates_presence_of :salon
  validates_uniqueness_of :salon
end

The new RSpec:

require 'spec_helper'

describe SalonWebProfile do
  it { is_expected.to validate_presence_of(:salon) }
  it { is_expected.to validate_uniqueness_of(:salon) }
end

The error:

2) SalonWebProfile should require salon to be set
     Failure/Error: it { is_expected.to validate_presence_of(:salon) }

     ActiveRecord::StatementInvalid:
       Mysql2::Error: Unknown column 'salon_web_profiles.salon' in 'where clause': SELECT  1 AS one FROM `salon_web_profiles`  WHERE `salon_web_profiles`.`salon` IS NULL LIMIT 1
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `block in query'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `handle_interrupt'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `query'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/ar-octopus-0.8.6/lib/octopus/abstract_adapter.rb:15:in `instrument'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:97:in `validation_errors'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:77:in `collect_messages'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:40:in `messages'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:48:in `has_messages?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:277:in `has_messages?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:273:in `errors_match?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:224:in `block in matches?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:220:in `each'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:220:in `none?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:220:in `matches?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:16:in `matches?'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validation_matcher.rb:56:in `disallows_value_of'
     # /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:126:in `matches?'
     # ./spec/models/salon_web_profile_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Mysql2::Error:
     #   Unknown column 'salon_web_profiles.salon' in 'where clause'
     #   /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'

I can not understand what is going on. Isn't it supposed to translate to salon_id when searching in database?
The version of rails is 3.2.22.5 and the RSpec version is 3.7.

That error doesn't happen when I'm using rails c and trying to create/access the salon_web_profile from salon and vice-versa.

Igorzovisk
  • 634
  • 6
  • 19
  • `salon` is the model, `salon_id` would be the attribute, no?. However if using Rails 3, the `belongs_to` relationship should be required by default (unless you specify other thing). – Sebastián Palma Dec 14 '17 at 12:45
  • I thought the `validates_presence_of :salon` would check if the salon really exists and `validates_presece_of :salon_id` would check only if the number is present, not mattering if exits a salon with this id or not. Am I wrong? – Igorzovisk Dec 14 '17 at 12:48
  • Your associations and test seem to be correct. Are you sure your test database has `salon_id` field? – Tony Vincent Dec 14 '17 at 13:42
  • also can you try using `validates :salon, presence: true` instead of `validates_presence_of :salon` and see if it changes anything – Tony Vincent Dec 14 '17 at 13:51
  • Tried it. Nothing changed. – Igorzovisk Dec 14 '17 at 14:06
  • This works please [refer](https://stackoverflow.com/questions/5689888/rails-validate-presence-of-association). [Nested models and parent validation](https://stackoverflow.com/questions/5144527/nested-models-and-parent-validation) also explains it in better way – Rahul Sharma Dec 14 '17 at 14:40
  • what version of, rails and rspec are you using? – Tony Vincent Dec 15 '17 at 03:56
  • It's in the main question. Rails: 3.2.22.5 and RSpec: 3.7. – Igorzovisk Dec 15 '17 at 11:05

1 Answers1

0

Try in rails test console i.e rails c test, if the same error occurs, then try running rake db:migrate RAILS_ENV=test

Dyaniyal Wilson
  • 1,012
  • 10
  • 14
  • 1
    Looking at the error message `salon_web_profiles` already exists, we can rule out the possibility of pending migrations. Also he has the references in the same migration file – Tony Vincent Dec 14 '17 at 13:44