Leonel *.* wrote in post #949930: > I have these tables... > > ACCOUNTS > id > company_name > > USERS > id > username > password > account_id (foreign key) > > > I have a create new user form. Each user is supposed to have an > account_id from an accounts table. Of course, on my user.rb model file I > have to validate for the presence of an account_id. How can I add the > account_id to the users table?
By setting it when you save the User object. The easiest way of doing that is probably to have a hidden field in your user creation form, assuming that the account ID is known at that time. > > 1) Maybe after logging in I store the account id on a session. Then, > when the user tries to add another user I grab it from the session? > > 2) Orrr... so I won't use a session, when the user tries to add another > user, I can query the user's row, get his account_id and then use it. > You don't need it separately in the session! If the current user is only adding users to his own account, then you've already got the account ID in the current user's info! current_user.account.users.create :name => 'Another user' > Which approach would work better? Do you even need to ask? The latter approach is simpler and avoids repetition. Always do the simplest and least repetitious thing possible! > > Now, once I have the account id, how can I go about adding it at user > creation? At the users controller? Wherever the User object is created. Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

