I am now doing a project on ruby on rails. I created a entity named product and i want to set a many to many relation to other entity named category.
script/generate scaffold product prd_name:string category:references
By doing this code only one to one mapping is possible.How can i set many to many without hard coding?
One of the products of the rails generate scaffold command is a database migration. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database.
To generate a fully working scaffold for a new object, including model, controller, views, assets, and tests, use the rails g scaffold command. Then you can run rake db:migrate to set up the database table. Then you can visit http://localhost:3000/widgets and you'll see a fully functional CRUD scaffold.
Scaffolding in Ruby on Rails refers to the auto-generation of a set of a model, views and a controller usually used for a single database table.
You should not expect to be able to generate your app via scaffolding alone. It is meant only to provide an example for getting started.
The most flexible kind of many-to-many relationship in rails is called has many through. This requires a join table which would typically be called 'categorisations' in this case. It would need a product_id
column declared as belongs to :product
and a category_id
column declared as belongs_to :category
. The three models (including the join model) would be declared thus:
# Table name: products
# Columns:
# name:string
class Product < ActiveRecord::Base
has_many :categorisations, dependent: :destroy
has_many :categories, through: :categorisations
end
# Table name: categories
# Columns:
# name:string
class Category < ActiveRecord::Base
has_many :categorisations, dependent: :destroy
has_many :products, through: :categorisations
end
# Table name: categorisations
# Columns:
# product_id:integer
# category_id:integer
class Categorisation < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
Note that I've named the columns name
rather than prd_name
since this is both human-readable and avoids redundant repetition of the table name. This is highly recommended when using rails.
The models can be generated like this:
rails generate model product name
rails generate model category name
rails generate model categorisation product:references category:references
As for generating the scaffolding, you could replace model
with scaffold
in the first two commands. Again though, I don't recommend it except as a way to see an example to learn from.
It's possible to generate a model with references with a command like this
$ rails generate model Comment commenter:string body:text post:references
See http://guides.rubyonrails.org/getting_started.html#generating-a-model
It's now possible to generate a scaffold with references with a command like this
$ rails generate scaffold Comment commenter:string body:text post:references
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With