We are development an announcement mechanism that works in such way:
– Users are nested attributes under an announcement. User’s emails are entered in the new announcement form as the nested attributes.
– Would like to work this way – When the announcement is saved, it needs to search user’s email w/ current users’ emails, if found, then deposit this announcement into that user’s profile and the user will be notified via email. If NOT, then a new user account is created and an email is sent for the user to claim his account.
When creating a new announcement, the rails saves all attributes tied w/nested attributes (this is great except when the user’s email already exists).
Since only user’s email is used to created the User’s account, how can we implement password later on (or auto create password to allow user to change later on)? We are using devise for authentication. Is there a way to use devise to perform this function?
How this problem can be solved ? Your help is greatly appreciated.
class Announcement < ActiveRecord::Base
attr_accessible :content, :users_attributes
has_many :users, :through => :awards
has_many :awards, :dependent => :destroy
accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:email].blank? }, :allow_destroy => true
end
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :middle_name
has_many :awards, :dependent => :destroy
has_many :announcements, :through => :awards, :dependent => :destroy
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true, :format => { :with => email_regex }, :uniqueness => { :case_sensitive => false }
end
You can implement an additional boolean attribute on your user model to indicate that the account has been claimed, and then not use the confirmable module of devise (since the user has effectively confirmed their email by following back a link). When a user arrives who’s account has not been claimed, fill in the authentication details at that point. Presumably you’d have an attribute in the Email linkback that lets you retrieve the appropriate record to populate.