Rails name rule question.

Asked 2 years ago, Updated 2 years ago, 144 views

Although there is a description of the name rule in Perfect Ruby on Rails

I have a question because the example is only when there is one word.

For example, if you do rails g model user,

Where rails should start with a capital letter on their own, add a capital letter, add a plural when you need a plural, and add a _ where there should be a _.

In this case, it's easy to understand because there's only one word, but what happens when there are two words?

Well, I can't think of a proper example

rails g model male_user

rails g model female_user

If you create it like this,

Inside the app/models,

A file named male_user.rb appears.

If you look at the name of the class inside the file, it becomes MaleUser.

If you use this somewhere else,

For example, in a migrate file,

t.integer :ale_user_id. Like this? Or :mailuser_id? Like this? rails g model [ ] Do I use it as I entered it?

ruby-on-rails-4

2022-09-21 17:37

1 Answers

In migration, it is better to define the relationship like belongs_to when registering the foreign key. In that case,

t.belongs_to :male_user

Use with .

This creates a field called mail_user_id in the table for that model.

However, since the class name is MaleUser, when creating a new record in seed, etc.,

MaleUser.create()

Use with .


2022-09-21 17:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.