Link Rails to non-model tables

I want to rewrite my project in Rails. It is currently written in PHP (CodeIgniter), I have come to the conclusion that I am writing more libraries and major extensions than I am writing new code. I studied some of the Rails manuals and I like what I see so far (although I feel that you have less control over what has been transferred). However, it seems that there is little information (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 makes no sense for me to create a model for this, right?

First question . How to create a migration to create a table without any model?

The second question . When this table is populated with data, how can I associate with it. for example, Link it to the User model (if possible) / user object so that I can find the user through the token (provided is_used = 0 ) and return the user data?

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

+4
source share
4 answers

Your current scenario requires a model, although when you need to store data such as Sport (football, tennis, cricket, swimming, basketball, etc.), you can save them as constants in your configuration initializers-> persistent , eg. SPORT_CATEGORIES = [["Football", "football"], ["Tennis", "Tennis"], etc.], Alternatively, if you have more ears for storage, you can create a model and then create default rows as in php.sql, 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.

+2
source

You need a model for this table. It makes sense for you to have a model for the user_verification_token table. To get the associations and all the other functions that you will need from this data, you need to create a model and associations for it.

To answer the first question: run rails generate migration your_migration_name

In the activity_type table that you mentioned in the comment on the original question, you might be interested in the RailsLookup stone to help automate some of the work needed to make effective use of lookup tables in Rails. There is a blog post describing it, and the source can be found on GitHub

+2
source

I suggest you create a model for this, since you will have full access to the user_verification_token table through the UserVerificationToken model, which will be associated with the user.

+1
source

Create a model because all checks can be performed on the model. for example: validates :user_id . Therefore, there is no need to include these restrictions when creating the table.

0
source

Source: https://habr.com/ru/post/1468799/


All Articles