Hot questions for Using Spree in activerecord

Top 10 Ruby Open Source / Spree / activerecord

Question:

I had this issue a couple of months ago, posted it on Spree's GitHub page, then figured out the solution myself and posted it on that same issue. Two months later, I'm still getting comments on that same issue from Googlers who had the same error message, so I figure it's worth posting my solution on this website as well just in case anyone is having the same problem but isn't looking on GitHub.

The issue: trying to create a brand new Spree app, following the exact instructions in Spree's documentation, gives the following error when you try to run spree install --auto-accept or rails g spree:install:

undefined method `raise_in_transactional_callbacks=' for ActiveRecord::Base:Class (NoMethodError)

Solution below...


Answer:

The problem: I'd created a new rails app with rails new, which had automatically created an app using rails 4.2.0beta. Then I had issues with gem incompatibilities, so I'd changed the version number of a few gems, including downgrading Rails back to 4.1.8.

BUT the config setting raise_in_transactional_callbacks is new in Rails 4.2.0, so in earlier versions of Rails it will raise an error. If you're getting this error message you need to remove the line config.active_record.raise_in_transactional_callbacks = true from config/application.rb.

Question:

Hi I have included given code

 Spree::Order.includes(:ship_address).where('ship_address.country_id = ?', 52).references(:spree_address).count

and i am facing given error

 (1.1ms)  SELECT COUNT(DISTINCT "spree_orders"."id") FROM "spree_orders" LEFT OUTER JOIN "spree_addresses" ON "spree_addresses"."id" = "spree_orders"."ship_address_id" WHERE (ship_address.country_id = 52)
PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "ship_address"
LINE 1: ...s"."id" = "spree_orders"."ship_address_id" WHERE (ship_addre...
                                                             ^
: SELECT COUNT(DISTINCT "spree_orders"."id") FROM "spree_orders" LEFT OUTER JOIN "spree_addresses" ON "spree_addresses"."id" = "spree_orders"."ship_address_id" WHERE (ship_address.country_id = 52)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "ship_address"
LINE 1: ...s"."id" = "spree_orders"."ship_address_id" WHERE (ship_addre...
                                                             ^
: SELECT COUNT(DISTINCT "spree_orders"."id") FROM "spree_orders" LEFT OUTER JOIN "spree_addresses" ON "spree_addresses"."id" = "spree_orders"."ship_address_id" WHERE (ship_address.country_id = 52)
from /Users/TopFormInvestment/.rvm/gems/ruby-2.1.4@skinnymint/gems/rack-mini-profiler-0.9.2/lib/patches/sql_patches.rb:160:in `exec'

Checked in spree model

belongs_to :ship_address, foreign_key: :ship_address_id, class_name: 'Spree::Address'
    alias_attribute :shipping_address, :ship_address

Please guide me how to solve this error.


Answer:

See if the following queries help.

Spree::Order.includes(:ship_address).where('spree_addresses.country_id = ?', 52).references(:spree_address).count

Question:

Products has Variants. I'm trying to fetch Variants that has cost_currency: "USD" A Variant may look like this: Spree::Variant id: 22, cost_currency: "USD">

Spree::Product.includes('variants') returns a lot of Variants, but if I try to filter only the ones that has cost_currency = true it fails.

This is what I did:

2.2.3 :077 > Spree::Product.includes('variants')
                 .where( variants: { cost_currency: "USD" })

But that throws an ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: variants.cost_currency error, which I don't understand why.

Full stack trace:

  SQL (0.5ms)  SELECT "spree_products"."id" AS t0_r0, "spree_products"."name" AS t0_r1, "spree_products"."description" 
  AS t0_r2, "spree_products"."available_on" AS t0_r3, "spree_pro
ducts"."deleted_at" AS t0_r4, "spree_products"."slug" AS t0_r5, "spree_products"."meta_description" AS t0_r6, "spree_products"."meta_keywords" AS t0_r7, "spree_products"."tax_catego
ry_id" AS t0_r8, "spree_products"."shipping_category_id" AS t0_r9, "spree_products"."created_at" AS t0_r10, "spree_products"."updated_at" AS t0_r11, "spree_products"."promotionable"
 AS t0_r12, "spree_products"."meta_title" AS t0_r13, "spree_variants"."id" AS t1_r0, "spree_variants"."sku" AS t1_r1, "spree_variants"."weight" AS t1_r2, "spree_variants"."height" A
S t1_r3, "spree_variants"."width" AS t1_r4, "spree_variants"."depth" AS t1_r5, "spree_variants"."deleted_at" AS t1_r6, "spree_variants"."is_master" AS t1_r7, "spree_variants"."produ
ct_id" AS t1_r8, "spree_variants"."cost_price" AS t1_r9, "spree_variants"."position" AS t1_r10, "spree_variants"."cost_currency" AS t1_r11, "spree_variants"."track_inventory" AS t1_
r12, "spree_variants"."tax_category_id" AS t1_r13, "spree_variants"."updated_at" AS t1_r14 FROM "spree_products" LEFT OUTER JOIN "spree_variants" ON "spree_variants"."product_id" =
"spree_products"."id" AND "spree_variants"."is_master" = ? AND "spree_variants"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND "variants"."cost_currency" = ?
[["is_master", "f"], ["cost_currency", "USD"]]
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: variants.cost_currency: SELECT "spree_products"."id" AS t0_r0, "spree_products"."name" AS t0_r1, "spree_produc
ts"."description" AS t0_r2, "spree_products"."available_on" AS t0_r3, "spree_products"."deleted_at" AS t0_r4, "spree_products"."slug" AS t0_r5, "spree_products"."meta_description" A
S t0_r6, "spree_products"."meta_keywords" AS t0_r7, "spree_products"."tax_category_id" AS t0_r8, "spree_products"."shipping_category_id" AS t0_r9, "spree_products"."created_at" AS t
0_r10, "spree_products"."updated_at" AS t0_r11, "spree_products"."promotionable" AS t0_r12, "spree_products"."meta_title" AS t0_r13, "spree_variants"."id" AS t1_r0, "spree_variants"
."sku" AS t1_r1, "spree_variants"."weight" AS t1_r2, "spree_variants"."height" AS t1_r3, "spree_variants"."width" AS t1_r4, "spree_variants"."depth" AS t1_r5, "spree_variants"."dele
ted_at" AS t1_r6, "spree_variants"."is_master" AS t1_r7, "spree_variants"."product_id" AS t1_r8, "spree_variants"."cost_price" AS t1_r9, "spree_variants"."position" AS t1_r10, "spre
e_variants"."cost_currency" AS t1_r11, "spree_variants"."track_inventory" AS t1_r12, "spree_variants"."tax_category_id" AS t1_r13, "spree_variants"."updated_at" AS t1_r14 FROM "spre
e_products" LEFT OUTER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = ? AND "spree_variants"."deleted_at" IS NULL
WHERE "spree_products"."deleted_at" IS NULL AND "variants"."cost_currency" = ?

Answer:

Please try doing

Spree::Product.includes(:variants).where('spree_variants.cost_currency = "USD"')

or

Spree::Product.includes('variants').where('spree_variants.cost_currency = "USD" ' )

or

Spree::Product.includes('variants').where(spree_variants: { cost_currency: 'USD' })

Question:

Once spree(3.1) work on local. Next is to push to heroku. I am sure have postgresql. But seem I have to manually do setup table using one of similar rails command. In the document I supposed to run

    rails g spree:install --user_class=Spree::User
    rails g spree:auth:install
    rails g spree_gateway:install

My first question is how to run those command for heroku

My solution is using these commands

   heroku run rake spree:install --user_class=Spree::User
   heroku run rake railties:install:migrations
   heroku run rake db:migrate
   heroku run rake db:seed
   heroku run rake spree_sample:load

Answer:

You don't need to run these commands again on heroku.

Just add the files generated by spree to git. Also make sure to add spree migrations to git and then you can just deploy it on heroku.

Followed by

heroku run rake db:migrate