Zack Shapiro
Zack Shapiro

Reputation: 6998

Something seems to be broken in my belongs_to, has_one relationship

I have a data model that looks like this:

A customer has subscription_id and setup_id as parameters. In some cases, customer will only have one of the parameters. In other cases, it will have both.

Currently, if I make a new customer through either the subscriptions flow or the setups flow, either Subscription.last or Setup.last will reflect the most recent customer that was created (with customer_id equalling the last customer created)

However, I am having the problem of Customer.setup_id or Custumer.subscription_id being nil in all cases.

Here's my code from both subscription.rb and setup.rb:

class Subscription < ActiveRecord::Base
  attr_accessible :status, :customer_id
  belongs_to :customer
end


class Setup < ActiveRecord::Base
  attr_accessible :status, :customer_id
  belongs_to :customer
end

And in customer.rb:

class Customer < ActiveRecord::Base
  attr_accessible :email, :name, :stripe_token, :subscription_id, :setup_id, :phone, :plan
  has_one :subscription
  has_one :setup
end

I'm not sure what I'm doing incorrectly here but I'd love it if the three data models could talk to each other correctly.

Edit: Is it bad that both setup and subscription belong to :user rather than :customer?

Edit 2: Updated the code of setup.rb and subscriptions.rb to correctly reflect the data model currently. And customer.rb is still not recognizing the correct setup_id or subscription_id

Upvotes: 0

Views: 120

Answers (1)

Cameron Martin
Cameron Martin

Reputation: 6010

class Subscription < ActiveRecord::Base
  attr_accessible :status, :customer_id
  belongs_to :customer
end

class Setup < ActiveRecord::Base
  attr_accessible :status, :customer_id
  belongs_to :customer
end

class Customer < ActiveRecord::Base
  attr_accessible :email, :name, :stripe_token, :phone, :plan
  has_one :subscription
  has_one :setup
end

customer = Customer.first
customer.subscription # Instance of Subscription that belongs to customer
customer.setup # Instance of Setup that belongs to customer

Upvotes: 1

Related Questions