From a practical and best practices perspective, should rails models that have HABTM association also have a defined model for example users_groups.rb. So, should you
script generate model ...
or simply
script generate migration ...
On one hand the join table is simply two fields and shouldn't have any methods of its own, on the other hand, in rails (model = table). So would it be wise to have it defined in models as well as in migrations?
Hi as for me if you have has_and_belongs_to_many you don't need any model but if you use has_many+:through you'll need one
Related
I'm starting out with ruby and rails, it's my 1st day, reading the guides.
I'm trying to generate a scaffold with various models, for instance Game and Platform. I'm trying to replace an existing, way outdated Symfony PHP project and picked Ruby/RoR for learning purposes, also because I was impressed with hotwire from the blog demo video.
A Game has_one Platform, a Platform has_many Games.
Or
type Game {
id: ID!
name: String
slug: String
platform: Platform!
}
type Platform {
id: ID!
name: String
slug: String
games: [Game]
}
For now I'd like to generate those 2 models with "rails g scaffold". I read on other questions that one has to use ":references" but I can't find that keyword anywhere in any guide of activerecord.
rails g scaffold Game name:string slug:string platform:references
is what I have so far. Is it even possible to define associations with foreign keys via the cli?
What is the typical workflow when generating models/scaffolds? Do you just start with the bare minimum of the model and manually edit the migration, add fk and associations?
I guess I'm more looking for a discord server for ruby/rails/other. I have so many questions but don't feel like this belongs here, because I would create question after question. But for now let's stick to this. (Side question, what is the de-facto standard when it comes to authn/z in rails? And another question, is there somelike that auto-slugifies, see the name and slug connection?)
The question is how to express model associations on the command line?
Is it even possible to define associations with foreign keys via the cli?
Yes.
You can define belongs_to assocations via the generators - as its the only assocation thats actually backed by a database column on this models table. You cannot define has_one, has_many or has_and_belongs_belongs_to_many via the generators - they must be added to the model later.
For now I'd like to generate those 2 models with "rails g scaffold". I read on other questions that one has to use ":references" but I can't find that keyword anywhere in any guide of activerecord.
It is not really a keyword. Rather references/belongs_to (the later is an alias) are column types (well kind of) when generating models or migrations.
If you run rails g model -h it details the options:
You can also consider references as a kind of type. For instance, if
you run:
bin/rails generate model photo title:string album:references
It will generate an album_id column. You should generate these kinds
of fields when you will use a belongs_to association, for instance.
It also adds belongs_to :album in the model when you use it with the model generator.
What is the typical workflow when generating models/scaffolds? Do you just start with the bare minimum of the model and manually edit the migration, add fk and associations?
Scaffolds are mainly useful for rapid prototyping or when you're learning as way to see the "Rails Way" of defining resources. They tend to generate way more code then you actually need and will outrun your tests.
Generators in general are just a tool and its often easier and faster to just edit the resulting files then to get the exact invokation of the generator right.
New to rails and wondering if there is a way to have all the associations you generate from a migration show up in that model. For example, working on a mock AirBNB app. If i were to generate a migration like so (with these models already created)
rails g migration AddListingsToNeighborhood listing:belongs_to
When I run rake db:migrate, my models are still empty. Just wondering if there is a shortcut to have these associations fill the models.
Thank you!
If you have not created the model (or can overwrite it), use
rails g model Neighborhood listing:references
If you have the model and just need to create the appropriate migration:
rails g migration AddListingsToNeighborhoods listing:references
The DB migration will not modify the model.
In these examples, Neighborhood gets the foreign key.
If you want Listing to have the foreign key (and have belongs_to), then you would need to reverse them:
rails g model Listing neighborhood:references
rails g migration AddNeighborhoodToListings neighborhood:references
FWIW, as you grow your apps, you will often be modifying existing models and adding relationships. This means manually adding belongs_to and running the migration.
I'm just playing with RoR and I've noticed that ActiveRecord associations such as has_many or belongs_to are decoupled from the database running behind, i.e., these association are set regardless of the the constraints set by the database. For example, I have a table comments and a table users and they are related through has_many and belongs_to statements (a comment belongs to a user and a user has many comments). However these associations still let me assign a comment to a, for example, non-existing user. The reason of this is that there's no foreign key defined in the database.
My question is: should I just rely on ActiveRecord's associations to handle data integrity or should I also add foreign keys in migration files?
Thank you.
Rails holds some conventions that enforcement of data integrity should be done in the application, not in the database.
To keep data integer on application-level, you can use model validations to enforce the presence of associations.
You have to add foreign keys to migration file to make your associations work correctly. With reference to the example mentioned, you have to add an attribute user_id to comments table. For more information on how Active Record Associations work, follow this rails guide.
I've been reading about Rails Migrations to help me start building a rails project.
I'm a bit confused on the generation of the files in db/migrate.
The way I've been designing my application is by starting with the models... outlining all of the objects that I'm going to have in the system as best I can. I would like to generate the migration files FROM these models automatically with the rails migration generator.
Yes, I know "creating migrations manually is easy." And I know I could do it manually, but what I don't understand is why the tool is separated from the pre-created models.
Based on my understanding from the article and other migration questions on SO, I can generate a migration like so:
rails generate migration SomeObj field:string some_other_field:integer
What I don't get is why I need to pass in the fields when my model already exists for SomeObj? Couldn't Rails detect it from the some_obj.rb and create the migration from there?
Also, when I have a more complex model, with has_many, belongs_to, and has_to_and_belongs_to_many relationships, it would be really nice if it autocreated the JOIN tables and fields with the correct names (e.g. foreign_obj_id, foreign_obj_ids)
In a previous project I didn't have to deal with migrations because I used Mongo+Mongoid - and collections were automatically generated due to the nature of how Mongo works (collections that doesn't exist get automatically created upon inserting or updating). Now with this Rails app I'm using Postgres (but I'm sure the same thing would happen with MySQL or any other relational database).
Anyway, is there no helper for this kind of thing? Do I have to create all these migrations manually?
You've got this backwards. You need to build your migrations first, and your models second. Migrations advance the state of the database. Your models reflect the current state of the database. There is not a 1-to-1 mapping of models to migrations.
I would like to generate the migration files FROM these models automatically with the rails migration generator.
Can't be done.
You use rails generate migration to generate the migration, which, almost as a side-effect, generates a stub model file. You cannot use your existing model file to generate the model's migration, because the model doesn't contain any information about the actual columns that make up the model. Rails would have to extract and imply all the necessary information from your associations/validations, and even then it would get most of the columns wrong.
Also, when I have a more complex model,... it would be really nice if it autocreated the JOIN tables and fields with the correct names
Again, you can't. You're responsible for defining your database schema, and the way you do so is by building migrations. You can do so manually, or via rails generate, but the process you should be following is building your migrations first, and your models second.
By way of example, here is a complete model, ready for production:
class MyModel < ActiveRecord::Base
end
That model definition might wrap a table that contains 10 columns or 1000 columns; you (and Rails) have absolutely no way of knowing based on the model definition.
Here's another model, which contains at least one column, but again, you have no way of knowing what kind of column:
class MyModel < ActiveRecord::Base
validates :code, presence: true, uniqueness: true
end
Is code a string or a number? Should there be an index on the column? There is absolutely no way of knowing.
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.