Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting references to a table through scaffolding in rubyonrails

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?

like image 367
Nithin Viswanathan Avatar asked Oct 29 '12 06:10

Nithin Viswanathan


People also ask

What is scaffold command?

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.

How do you make scaffold rails?

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.

What is a scaffold Ruby?

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.


3 Answers

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.

like image 111
noodl Avatar answered Oct 09 '22 22:10

noodl


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

like image 18
Rui Castro Avatar answered Oct 09 '22 22:10

Rui Castro


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
like image 10
user3402754 Avatar answered Oct 09 '22 22:10

user3402754