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
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
Related
I have two models Person and Address:
class Person < ApplicationRecord
has_one :address
end
class Address < ApplicationRecord
belongs_to :person
end
How do I validate that each points to the other?
I want to do something similar to:
class Person < ApplicationRecord
has_one :address
validates :address, presence: true
end
Of course this doesn't work because address is not an attribute on Person.
What is the correct way in Rails to ensure that a record has a valid association with another record?
class Person < ApplicationRecord
has_one :address, required: true
end
class Address < ApplicationRecord
belongs_to :person
end
See the required option in the options section of https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_one
belongs_to in Rails 5 is now automatically required so you don't need to put anything different inside of Address.
You need to use validates_associated :address to validate associations
I have a has many through relationship in my app:
Shows has many Bands through => Lineups
Bands are unique by :name
class Show < ActiveRecord::Base
attr_accessible :city_id, :title, :dateonly, :timeonly, :image, :canceled, :venue_attributes, :bands_attributes
belongs_to :city
belongs_to :venue
has_many :lineups
has_many :bands, through: :lineups
has_and_belongs_to_many :users
end
class Lineup < ActiveRecord::Base
belongs_to :show
belongs_to :band
end
class Band < ActiveRecord::Base
attr_accessible :name, :website, :country, :state
has_many :lineups
has_many :shows, through: :lineups
validates :name, presence: true
validates_uniqueness_of :name
before_save :titleize_name
private
def titleize_name
self.name = self.name.titleize
end
end
New Bands are created like this:
(lets say we have a show record already saved called s1)
> s1.bands.new(name: "Wet Food")
> s1.save
Right now this will only save if a band named "Wet Food" doesn't already exist
In which model is the best place to do a Band.find_or_create in this relationship so that an existing band can be used if one with the same name exists?
This is generally the type of call that would go in a Controller (or maybe a service object), but not in a Model. It really depends on the particular user flow that you're trying to accomplish in your app. Basically, where ever you are already using s1.bands.new, you could use this instead :
s1.bands.where(name: 'Wet Food').first_or_create
i need to create a form, which will create object which has another two objects as attributes, but those objects should be available from a dropdown list that contains templates of those objects.
class User < ActiveRecord::Base
accepts_nested_attributes_for :adresses, :profiles
end
class Address < ActiveRecord::Base
attr_accessible :city, :country
belongs_to :user
end
class Profile < ActiveRecord::Base
attr_accessible :nickname, :password
belongs_to :user
end
tricky part might be, that User has no column 'address_id' or 'profiles_id', everything should go to the Profile and Address, which are being created in the same moment as the User (they have the same attributes as their templates)
I could really use some help, dont expext full code solution, but some hints would be nice
Try this setup:
class User < ActiveRecord::Base
has_one :address
has_one :profile
accepts_nested_attributes_for :address, :profile
attr_accessible :adress_attributes, :profile_attributes
end
class Address < ActiveRecord::Base
attr_accessible :city, :country
belongs_to :user
end
class Profile < ActiveRecord::Base
attr_accessible :nickname, :password
belongs_to :user
end
See doc
I have models Productor, Company and User
I want that a productor AND a company have both an user. An user that belongs to a Company belongs only to this company. An user that belongs to a Productor belongs only to this productor.
so the tables I want to be this way
productor company user
--------- -------- ------
id id id
name name email
user_id user_id password
I tried to do this with has_one association but I got this error
no such column: users.produtor_id: SELECT "users".* FROM "users" WHERE "users"."produtor_id" = 1 LIMIT 1
and my model follows
class Produtor < ActiveRecord::Base
attr_accessible :borndate, :cpf_cnpj, :is_company, :name, :rg
has_one :user
...
class User < ActiveRecord::Base
attr_accessible :email, :password
...
end
I think you have has_one and belongs_to mixed up.
Take a look at Is it a belongs to or has_one association here
with the columns you have:
class Productor < ActiveRecord::Base
belongs_to :user, :inverse_of => :productor
end
class Company < ActiveRecord::Base
belongs_to :user, :inverse_of => :company
end
class User < ActiveRecord::Base
has_one :productor, :inverse_of => :user
has_one :company, :inverse_of => :user
end
Have you made the association in your User model yet?
class User < ActiveRecord::Base
attr_accessible :email, :password
belongs_to :productor
...
end
My models look something like this:
class User < ActiveRecord::Base
attr_accessible: :name
has_many :reviews
end
class Product < ActiveRecord::Base
attr_accessible: :name
has_many :reviews
end
class Review < ActiveRecord::Base
attr_accessible: :comment
belongs_to :user
belongs_to :product
validates :user_id, :presence => true
validates :product_id, :presence => true
end
I am trying to figure out what the best way is to create a new Review, given that :user_id and :product_id are not attr_accessible. Normally, I would just create the review through the association ( #user.reviews.create ) to set the :user_id automatically, but in this case I am unsure how to also set the product_id.
My understanding is that if I do #user.reviews.create(params), all non attr_accessible params will be ignored.
You can do:
#user.reviews.create(params[:new_review])
...or similar. You can also use nested attributes:
class User < ActiveRecord::Base
has_many :reviews
accepts_nested_attributes_for :reviews
...
See "Nested Attributes Examples" on http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html.
It seems you would like to implement a many-to-many relationship between a User and Product model, with a Review model serving as a join table to connect the two with an added comment string. This can be accomplished with a has many through association in Rails. Start by reading the Rails Guides on Associations.
When setting up your Review model, add foreign keys for the User and Product:
rails generate model review user_id:integer product_id:integer
And set up your associations as follows:
class User < ActiveRecord::Base
has_many :reviews
has_many :products, through: :reviews
end
class Product < ActiveRecord::Base
has_many :reviews
has_many :users, through: :reviews
end
class Review < ActiveRecord::Base
# has comment string attribute
belongs_to :user
belongs_to :product
end
This will allow you to make calls such as:
user.products << Product.first
user.reviews.first.comment = 'My first comment!'
Here's how you would create a review:
#user = current_user
product = Product.find(params[:id])
#user.reviews.create(product: product)