I have bassicly 3 tables : Users (email, password), Contacts (name, phone), Relations (user_id, contact_id, level) .
When a user creates a new contact, i want him to be associated to it. The association has 1 to 3 as level of "friendship".
I use a form to input the level in my contacts#create controller.
For now, i have this which works great
def create
@contact = Contact.new(params[:contact])
if @contact.save
#@relation = Relation.new(:user_id => current_user.id, :contact_id => @contact.id, :level => params[:relation])
#@relation.save
redirect_to root_url, :notice => "ok!"
else
render "new"
end
end
I was thinking of moving the relation creation to my contact model to do something like this :
after_create { Relation.create(user_id: current_user.id, contact_id: self.id, level: params[:relation]) }
Of course, this does not work, but you get the idea. Would it be good to that in the model or can i keep it as i do for now
cheers
Something like this? Basically just create the relation and contact all in one, associated to the current_user.
current_user.relations.create(contact: Contact.new(params[:contact]), level: params[:relation])
Don't move it to an after_create. If anything create a function somewhere that accepts a user, a contact and a relation.