Reputation: 3324
I am creating an active record object like so:
shop = ShopifyShop.create(shopify_domain: shopDomain, primary_domain: shopObj.domain, shopify_token: token, user_id: userId) if !shop
When the an object with the same primary_domain
is created, the record is not stored in the database and the object is created with no primary key
so when i do:
p shop
i get #<ShopifyShop id: nil, shopify_domain: "***.myshopify.com", shopify_token: "***", created_at: nil, updated_at: nil, user_id: 45, primary_domain: "***.myshopify.com">
I checked to see if it was related to constrain maybe but theres only a primary key constraint in the table, i am not sure what is causing this.
Anyone know what could be happening?
Upvotes: 0
Views: 36
Reputation: 346
I'm pretty sure that your object is simply invalid and therefore was not saved to database. That's why it has no id
. You can check if there are any errors by:
shop.errors.any?
shop.errors.messages # => display errors
If you want to raise RecordInvalid
error if validations fail, use create!
instead of create
.
Upvotes: 2
Reputation: 5313
One of the validations you have for your shop caused your object to not be saved. create
returns the object regardless of whether or not it was saved.
In order to investigate further:
shop.persisted?
# => false
shop.errors
# => will display errors of your object
Upvotes: 2