Where does Devise store passwords? - ruby-on-rails

This is just a question out of my own curiosity, I would assume that it stores them in the user's table, but maybe it doesn't perhaps this isn't secure? if it doesn't store it in the users table where does it go, and if you know...why?
Thanks :)

Devise stores passwords and other data inside the table associated with the model you specify during setup. During setup you pass it a model name:
# MODEL is a placeholder for the model you want to use with Devise, usually 'User'
rails generate devise MODEL
The generator then creates a migration that alters the table associated with that model. So if you passed it User, the users table will be altered.
Have a look at the migration files inside Devise.

Related

Add a Time column to Devise and have it updated manually from controller in Rails 4

I have a need to see when an user started a subscription. I need to add a new column named subscription_started_time in the users table created by a Devise generated migration.
I can easily add the column, but how do I populate it? I obviously shouldn't use strong parameters, because the time input will be added straight from the controller and it wont be coming through a form.
What would be the best way to handle input straight from Ruby when the user creates/updates an account?
If you want to set User's :subscription_started_time at it's creation, you can use one of ActiveRecords hooks - before_create.
Let's consider an example:
class User < ActiveRecord::Base
# ...
before_create do
self.subscription_started_time = DateTime.new
end
end
Please, check documentation for more hooks available for your ActiveRecord models.
Hope that helps!

What's the proper way to create a database referencing a user using the devise gem?

I'm creating a simple webapp that tracks calories as a way to learn Ruby on Rails. I'm using the gem devise for users to sign in. My next step is to generate a model. I was going to use:
rails generate model Tracker calories:integer consumed_on:datetime
The problem though is that I don't know how to relate this data to the signed in user. What am I missing from this generate model command?
rails generate model Tracker user_id:integer calories:integer consumed_on:datetime
This will generate your model with a foreign key in the database referencing the user table.
Of course this is not all you have to do, you have to put other code in your model and controller to combine those two columns, create the views, update the config/routes.rb file ..
Also:
Don't forget to run the rake db:migrate to make the relational table in the database.
You can see how its done here (your Tracker model/table is theirs Micropost):
http://ruby.railstutorial.org/chapters/a-demo-app#sec-microposts_resource

Unique IDs between Users and Admins with Devise Rails

I have installed Devise and created Users and also Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role
Now I need a little help. When adding the Admins it creates a different table, and the potential arises that the admins and regular users could have the same ID's.
So how exactly do I got about grabbing information from users and admins? Say for example I want to display all users? Do I have to traverse the users table and then the admins table?
Or if I am displaying a post. How will I know which table to look for to get the user or admin info?
I know I can just add a role column to the users table but I wanted to avoid this.
I hope this makes sense haha
I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.
class NormalUser < User
end
class Admin < User
end
The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.
To create an admin use Admin.create(...) and to create a normal user use NormalUser.create(...) where the dots are the attributes of the Admin and the NormalUser

What is the common way to define models in Rails application?

All of the tutorials I've seen so far for RoR have shown me generating models like:
rails generate User name:string placeofbirth:string
This generates a class for the model, and only actually references an attribute if I apply a validation of some kind.
So my question is, how do I use a 'code' first approach when creating my models. Or is it the rails way to just right down on paper the attributes you want, run the generate command with each attribute you want and it's type, then run the rake db:migrate command?
I'd love some more proven patterns on this subject because so far the way I've seen seems too empty.
Yes, this is the rails way- migration comes first and generates the code and the database- and the model class inspects the database to see what fields are there and make accessible via methods.
You can do gem install annotate_models if you want to get some comments in your model class with the attribute names and types.
See here for an example: https://github.com/ctran/annotate_models
Rails uses an active record pattern for models which basically means that a model object will automatically map each DB column to an attribute so you don't have to specify all attributes in the model. It's a feature, but I agree that it might not be perfect for everyone. If you're using Rails 3 it should be easy to use another ORM of your choice if ActiveRecord's approach doesn't suit you. Here are some alternative ORMs that you could use.
Usually when you are developing some database backed web application, you know the database design(name of the tables, name of the columns in those tables and associations between different tables) beforehand.
Rails, as mentioned by maarons in his answer, uses Active Record pattern. Your models are classes that represent a table in your database, an instance of your model class a row in that table and different attributes of an object represent values under different columns in the same table.
When you create a model, usually, you are creating a class that represents one of the tables in your database. And while you are creating a model, you are also going to create a table in your database. That means knowing the name of the table and columns within that table.
So to answer your question, you must know all the columns, required for the time being, that will be in your tables. And hence available as attribute methods for your model objects. You specify these columns to added in the table in the migration generated by rails generator while generating this model. This is what usually everyone does.
You can take a code first approach by creating a class, without running the rails model generator,under app/models/ but not inheriting it from ActiveRecord::Base. As you move forward in your development, you can generate migrations by $ rails generate migration MigrationName and creating table and adding columns using [add_column][2]to that table as required. Once you have created a table for this model, you will have to inherit that model from ActiveRecord::Base so that you can get all the Rails magic in your application.

Ruby on Rails working with pre-existing database. Rails makes everything plural

What is the deal with this? I'm working with a pre-existing that I did not do myself. Everything in the database is labeled in singular form. user, security, spec, etc. I guess the right way would be users, securities, specs. At least that's what ruby on rails try's to lookup when I generate a scaffold .
How do I specifically state to use user instead of users in the sql. I don't see anywhere in my project where it is looking up the sql. I mean if my model is user you would think it would try to lookup user. Instead of users.
Thanks for any help.
You need set_table_name :name_of_the_table in your model (source).
So:
class User < ActiveRecord::Base
set_table_name :user
end
The reason they use plural for the table and singular for the model is because an instance of the model represents one user, whereas the table contains all the users. It's just to make it more readable and logical.
You can specifiy the table name:
How do I explicitly specify a Model's table-name mapping in Rails?

Resources