Jackson Cunningham
Jackson Cunningham

Reputation: 5073

Rails: first_or_create not working

Very strange issue with first_or_create. Consider the following method:

def self.store(session)
    shop = self.first_or_create(shopify_domain: session.url, shopify_token: session.token)

    binding.pry

    shop.save!
    shop.shopify_domain
  end

When I pry into this method, I can call session.url to get domain2.myshopify.com and session.token to get 22222

But when I call shop, I get a shop where shopify_domain: domain1.myshopify.com and shopify_token: 11111.

Any idea why this would happen? It seems bizarre.

Upvotes: 1

Views: 780

Answers (1)

penner
penner

Reputation: 2737

shop = self.where(shopify_domain: session.url, shopify_token: session.token).first_or_create(shopify_domain: session.url, shopify_token: session.token)

You are just getting the first one in general. Like calling .all.first

Upvotes: 6

Related Questions