rails g model with associations on the cli and other questions - ruby-on-rails

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.

Related

Rails Fixtures - what do these mean?

Context
When I generate the fixture file, rails generate model Post title:string text:text it shows:
one:
title: MyString
text: MyText
two:
title: MyString
text: MyText
Question
I was wondering why are there 2, and why are they named "one" and "two".
Here I see that names like "google", "rubyonrails", and "parent/child" are used; whereas, following the tutorial for generating the posts model, it generates just one and two...
Upon more research, I found that I might also be interested in db/migrate files. My current theory is that these files create the structure of my data... so I'm not so sure what fixtures does.
Reason
I'm trying to create a "Student" model using
rails generate scaffold student
but it doesn't seem to have :name as one of its keys. I'm looking into fixtures to add data columns.
Just some quick notes on your question:
Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.
By default, Rails provides two fixtures named 'one' and 'two' every time. You can change them as you so please, Also, the data that goes into the fixtures is made when you pass in the keys for the database columns you want when using the generator. In the the first example were you used rails g model Post title:string... you created a model called Post and passed in two keys: :title and :text.
Answer:
As for your last question, you can quickly resolve the issue by
a) Deleting the old scaffold by typing the following in your command line:
rails d scaffold Student
b) Creating it again but this time with the keys you want:
rails g scaffold Student name:string
I'll start by saying that the code generated by the rails generate command is intended to be a starting point for parts of your application, helping you get going quickly.
That said - fixtures are intended for use in unit tests. They give you a way to generate a set of objects already existing in the system, so you don't have to create them by hand at the beginning of your test.
In this case, one and two are just placeholders. In a real app, you'd replace them with names that were more meaningful in your tests.
If you're looking to add columns of data in your app, fixtures probably aren't the right approach. They're really meant for use in tests, nothing more.
Try this:
As for your last question, you can quickly resolve the issue by a) Deleting the old scaffold by typing the following in your command line:
rails d scaffold Student
b) Creating it again but this time with the keys you want:
rails g scaffold Student name:string

Rails Generating Migrations From Model

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.

Composite entity scaffolding in Ruby on Rails

I am new to Ruby on Rails. So far, I have only created CRUD operations by using scaffolding. Now, I need to integrate two entities into single form using scaffolding, not by hard coding it.
How can we scaffold two entities together using script?
I have two entities, Student_address and Student_phnumber. I want to scaffold these two entities into a single form where I can do CRUD operations, and I want to achieve this by scaffolding.
Student_address is an entity consisting of Hse_name, Street_name, etc.
Student_phnumber another entity consisting of ph_number, type, etc.
I want to scaffold these two entities together.
Scaffolding is nothing more than a generator set up to model a complete basic resource. There are many other generators, other than scaffolding, that come with Rails by default. None of them are set up to generate resources for a set of related models. I suspect that a large part of this is because of the wide range of methods to express such a relationship make creating a generic UI virtually impossible. Also, scaffolding is more set up to get you up and running very quickly, with the intention of being modified to suit your needs. These modifications are usually fairly involved for any non-trivial application. There are many 3rd party generators out there, including the popular nifty generators, but none that create the kind of code you want to generate, as far as I know. If this is a relationship that you need to set up frequently, you may consider creating a generator of your own to handle the task - it's actually pretty easy. A good way to do it is to implement your ideal case, then create a generator from it.
Edit:
Also, you may wish to adjust your variable/attribute names. They should be lowercase and underscored, so Street_name would become street_name. Arbitrary abbreviations also make it very hard to code/maintain, so Student_phnumber would be better expressed as student_phone_number. The reason for doing this, apart from consistency (ph_number vs Student_phnumber, for example), is that Rails actually uses the casing and spacing in internal methods like these.
Let me see if I understand you.
The model/entity relationship you are describing is:
student
address
- house_name
- street_name
- etc
phone_number
- number
- area_code
- etc
You want to:
a) automatically generate the models
b) automatically generate a controller/view with a form to create a student, including fields to set up the address and phone number
Okay. b) can't be done via a Rails scaffold. You can, however, use the ActiveSupport gem (docs here) to achieve this. Here's what you do:
gem install active_scaffold
rails g active_scaffold Student name:string
rails g active_scaffold PhoneNumber area_code:integer number:integer student_id:integer
rails g active_scaffold Address first_line:string second_line:string student_id:integer
The only manual work you'll have to do here is pop into the models and add the relationships:
Address
belongs_to :student
PhoneNumber
belongs_to :student
Student
has_one :address
has_one :phone_number
What ActiveScaffold will do is automatically produce for you a view like this:
Fill in that form and your models will all be saved and linked together!
Is this what you are looking for?
rails generate scaffold Student student_address:string student_phnumber:string

What is the common way to define models in Rails application?

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.

rails HABTM migrations

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

Resources