Pregunta

He creado un modelo llamado Userhastesages basado en algunas publicaciones.Creo que esto parece estar creando algunos desafíos para mí:

  • ¿Cómo lo hago referencia con has_many?¿Debo usar has_many :user_has_messageses?

  • ¿Cómo hago uniones?He intentado el usuario.Joins (: user_has_messages) y está triste: (

    Pregunta:

    ¿Debo cambiar de alguna manera el nombre a UserHastage, y si es así, cómo?

    Si lo mantienes como plural, ¿cómo manejo estos casos?

¿Fue útil?

Solución

You could add a new migration

rails g migration rename_user_has_messages

inside it you write:

class RenameUserHasMessages < ActiveRecord::Migration
  def self.up
    rename_table :user_has_messages, :user_messages
  end

  def self.down
    rename_table :user_messages, :user_has_messages
  end
end

(the table is always plural)

Run the migration.

Rename your file from user_has_messages.rb to user_message.rb, and rename your class from UserHasMessages to UserMessage.

Done :)

Otros consejos

You can use the same syntax you use with legacy tables:

class OtherClass < ActiveRecord::Base
  has_many :user_has_messages, :class_name => 'UserHasMessages'
end

Avoid class names that end with an S, like the devil avoids holy water. The name UserHasMessages is a very poor choice. You do not create a db table to check for something. Instead, you have a User model, a Message model and a UserMessage model. Then, if you want to check user messages, you just create a method that does that. The association should be :

User has many messages through user_messages

and you would get user messages like current_user.messages .

I highly advise you to change your design to the one i described :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top