Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do i save a field in a separate table when using devise?

Alan Reid asked in Rails

I am using devise for my users, during sign up i have an extra field i want to store in a different table in my Postgres DB. How would i go about storing this extra field?

So a company has_many users, and users belongs_to company.

I have a boolean field in company called is_brand

In the devise sign up form i want to be able to store the selected option of is_brand and create a row in my companies table in the correct field in that table.

Reply

You can use fields_for in the form to have form set both the User and Company objects together. Your User model will need accepts_nested_attributes_for :company on it to properly assign the attributes.

I did an example on that with this episode using the forum as an example: https://gorails.com/episodes/forum-nested-attributes-and-fields-for

Reply

Cheers buddy

Reply

Definitely watch Chris' video on it, I believe that's where I finally figured it all out.

The keys things that stumped me were ensuring

accepts_nested_attributes_for :company

is properly set in the User model, and then ensure your sanitizers are set correct in registrations_controller.rb or application_controller.rb (depending on your setup):

devise_parameter_sanitizer.permit(:sign_up) do |user|
            user.permit(:email, :password, :password_confirmation, :is_brand)
        end

Then in your sign-up form:

<%= form_for resource, as: resource_name, url: registration_path(resource_name),
                             html: { class: "form-horizontal", novalidate: true } do |f| %>

and just call the :is_brand as if it's a normal field..

<%= f.select(:is_brand, options_for_select(Company.is_brands.keys.to_a)) %>

You may need to change your select method - this one is based on an enum being defined in the model to define companies

I think that's all there was to it... good luck! :)

Reply

Cheers Jacob, much appreciated haha There is so much to pick up haha wish i had of started sooner, and not wasted time on .net lol

Reply

Hah I hear ya - I've been doing web "stuff" for well over 15 years but always stuck to CMS's like Joomla or WP and never really learned how to program, only just how to hack around what someone else did to make it fit my needs... only in the past year did I pickup Rails and boy do I wish I would have started much sooner!

It's all good though, we're here now and as long as Chris keeps pumping out these awesome videos everything will be right as rain! :)

Reply

Chris has done some awesome casts. Some of the best I have seen, well worth the subs we pay. Plus the community is pretty cool too.

I've specialised in front most of my time, with .net backend. It got to the stage where I had really lost my passion for backend development. Rails really sparked that passion again, and now I am really loving the full stack again.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.