I two User and Submission models as follows:
class User < ActiveRecord::Base # Associations has_many :submissions accepts_nested_attributes_for :submissions # Setup accessible (or protected) attributes for your model attr_accessible :email, :name, :role, :submission_ids, :quotation_ids, :submissions_attributes validates :email, :presence => {:message => "Please enter a valid email address" } validates :email, :uniqueness => { :case_sensitive => false } end class Submission < ActiveRecord::Base belongs_to :user attr_accessible :due_date, :text, :title, :word_count, :work_type, :rush, :user, :notes validates :work_type, :title, :text,:presence => true validates :text, :length => { :minimum => 250 } validates :word_count, :numericality => { :only_integer => true } end
I have a form that collects the data required by these two models. User Controller:
def index @user = User.new @user.submissions.build end def create @user = User.where(:email => params[:user][:email]).first_or_create(params[:user]) if @user redirect_to :root else render 'pages/index' end end
What I want to do is first check if the user exists in the system by email. If so, I want to create a view for this user. Otherwise, create a user and send it at the same time.
I am confused about how to do this using the first_or_create method.
Any help was appreciated.
chell
source share