Invalid single-table inheritance type: Admin is not a subclass of User

This error is bizarre. This error wasted my couple of hours; here is the detail

screenshot-from-2016-12-20-15-03-01

Solution

Here is the situation – I have used STI technique for models like Admin, VO, GO where all are derived from User model.

Lately I added ActiveAdmin plugin in my app and I was required to integrate it using the Admin model of course. For that purpose ActiveAdmin installer created a file called app/admin/admin.rb

Actual Reason

I suspect that, the similarity of filenames admin.rb might be the cause of the error. Because, the models and the ruby files in app/admin dir are lazy-loaded[loaded on need].

When I renamed the file app/admin/admin.rb to admins.rb ; then restarted the server, it started working back again.

Another way

The cause of this error is found to be the namespace collision. ActiveAdmin creates a module for namespaces (by default Admin). Rails could not find the Admin model but found Admin namespaced-module instead; this is what the error is saying. So, change the namespace like

#
# To set no namespace by default, use:
#   config.default_namespace = false
#
# Default:
config.default_namespace = :admins # instead of :admin

Rails : Formtastic : Single Table Inheritance : Issues and solution

I have been using Formtastic from the beginning and then some feature changes came across my application. I had a model called `DrugTest` then I also needed to implement additional drugtest (hair test and alcohol test). So I thought STI would be a perfect match to my scenario.

I created two classes `AdditionalHairDrugTest` and `AdditionalAlcoholDrugTest` derived from the model `DrugTest`

# == Schema Information
#
# Table name: drug_tests
# type :string
# ...
class AdditionalAlcoholDrugTest < DrugTest
 belongs_to :pact
 belongs_to :user
 default_scope -> {where(type: :AdditionalAlcoholDrugTest)}

 def is_additional?
   true
 end
end

Now I encountered this error Continue reading

Issues with Stripe : Mock and solutions

undefined method `api_key=’ for StripeMock:Module

Looks like we dont need to pass API_KEYs if everything we are going to mock everything.

Stripe::InvalidRequestError: No such plan: thepact_monthly

Create a mock plan like

plan_params = {:id => 'thepact_monthly',
               :name => 'StripeMock Default Plan ID',
               :amount => 1900,
               :currency => 'usd',
               :interval => 'month'}
stripe_helper.create_plan(plan_params)

Stripe::InvalidRequestError: You must supply a valid card xoxo

You need to pass ‘source’ attribute as well

subscription = customer.subscriptions.create(
    {
        plan: 'thepact_monthly',
        source: stripe_helper.generate_card_token({name: 'Shiva'})
    })

Rails : Capybara Webkit : Issue : Multiple JS events in single example

I recently moved from Selenium Web Driver to Thoughtbot’s Capybara Webkit as JS Driver for Capybara. The main reason is, webkit is super fast in running the test suite and its headless (no browser pops up ) .

Problem I faced

I was changing the option in select box and expecting the change in span’s content. Actually some sort of calculation should have been triggered. Webkit was not triggering the ‘change‘ event properly.

Continue reading