I have a user table and an organization table. When you sign up for a user, the application also creates a new organization for you. I did this by putting this code in user.rb:
class User < ActiveRecord::Base
before_create :create_organization
belongs_to :organization
private
def create_organization
self.organization = Organization.create :name => self.name
end
end
But there will be some cases where parameters will be passed on through the URL, such as
http://localhost:3000/sign_up?organization_id=10
How do I create some sort of if-then statement so that if the organization_id parameter is present, the user model DOESN’T create a new organization.. But instead submits the existing organization_id parameter as the organization_id for the user?
Simple add check into your model,
And in your controller simply set
organization_idfromparams.