Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 146 Vote(s) - 3.36 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting references to a table through scaffolding in rubyonrails

#1
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?
Reply

#2
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](

[To see links please register here]

). 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.
Reply

#3
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
Reply

#4
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 [

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply

#5
We can not do this through scaffolding. We must edit the model of the class to set many to many relationship.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through