4

I'm looking to rewrite my project in Rails. It's currently written in PHP (CodeIgniter), I've come to the point where I'm writing more libraries and core extensions than I'm writing new code. I've been looking at some Rails tutorials and I'm liking what I see so far (even though I feel you have less control of what's been passed around). However it seems there's little information out there (maybe I'm not looking in the right places) on database tables without models.

For example, I need a table called user_verification_token

CREATE TABLE IF NOT EXISTS `user_verification_token` (
  `user_id` int(11) NOT NULL,
  `token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `is_used` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

It doesn't make sense for me to create a model for this, right?

First question: How do I generate a migration to create a table alone without any model?

Second question: When this table is filled with data, how do I associate with it. e.g. Associate it with the User model (if possible)/a user object so I can find a user by a token (provided is_used = 0) and return the user's data?

I apologize in advance if this is a newb question, I can do all these comfortably with PHP but I don't know how to do this on Rails.

Simone
  • 20,302
  • 14
  • 79
  • 103
Saff
  • 563
  • 1
  • 10
  • 21
  • Can a user have multiple tokens? If so, why? – mattr- Mar 12 '13 at 19:11
  • This table handles all tokens sent to the user's email, 1.to reset password 2.to confirm email 3.change email etc Oh and there's a token_type col which state's what the token is for. If a user has used the token is_used field is updated. Anyways this isn't the only scenario i don't want to create a model for, they're quite a few. for example, activity_type with just type_id and name. – Saff Mar 12 '13 at 19:17
  • Wow seems like i'm not the only one that needs to find a way to do this. Thanks for the vote ups. – Saff Mar 13 '13 at 19:22

4 Answers4

3

You need a model for this table. It does make sense for you to have a model for the user_verification_token table. In order to get the associations and all the other functionality you'll want from this data, you need to create a model and the associations for it.

To answer the first question: run rails generate migration your_migration_name

For the activity_type table you mention in the comment on the original question, you might be interested in the RailsLookup gem to help automate some of the work required to make good use of lookup tables in Rails. There is a blog post describing it and the source can be found on GitHub

mattr-
  • 5,333
  • 2
  • 23
  • 28
2

Your current scenario does require a model, although when you need to store data like categories e.g Sports(football,tennis,cricket,swimming,basketball,etc), you can store them as constants in your config->initializers->constant eg. SPORT_CATEGORIES = [["Football","football"],["Tennis","tennis"],etc], alternatively if you have more cols to store you can create a model then create the default rows as you would in a php .sql file but in ruby of course :) For example:

sport_categories = [
    {:category => "football", :category_type => "manly" },
    {:category => "Tennis", :category_type => "manly" },
    etc
]

sport_categories.each do |attributes|
    Model_name_here.find_or_initialize_by_category(attributes[:category]).tap do |p|
        p.category_type = attributes[:category_type]
        p.save!
    end
end

Then you run rake db:seed.

Tommy Adey
  • 806
  • 6
  • 12
1

I will suggest you to create model for this, as you will have complete access to user_verification_token table via UserVerificationToken model which will be associated to user.

Ramiz Raja
  • 5,942
  • 3
  • 27
  • 39
  • 2
    This is sort of a general question. You can't create a model for every table in you database. How do you do the associations when you don't have a model for a table? – Saff Mar 12 '13 at 19:07
  • 1
    Actually, with Rails, not having a model for every table in your database is a very rare case. It's the way that Rails works. – mattr- Mar 12 '13 at 21:16
0

Create a model because all the validations can be done in a model. eg:validates :user_id. So there is no need to include those constraints while creating the table

Sai Ram Reddy
  • 1,079
  • 13
  • 14