I have a category, a subcategory, and a product model.
I have:
in the category / next subcategories subcategory has many product subcategory related category_ category product_o subcategory
is there any way As
is the category / several projects via subcategories
?
The 'normal' railway route will not work because "subcategory" is not for the product, so there is no sub-category_ID field in the product. Instead, I need a query
from SELECT * in the products where the ID id class. Category_ID
Is there any way to do this?
Thanks,
Nicholas Hock Isza
This is the 'normal' Ruby on the way of the railway, the database you describe will look something like this in the way. If your database is not structured in this manner, then I would like to tell you how it is done for organizations in Rail Ruby, because this is the right way (and you use the t.references: category
It should have been made to make it difficult to mess up your references since migration).
+ ---------------- + + ---- ------------ + + --- ------------- + | Categories | | Subcategories | | Products | + ---------------- + + ---------------- + ------------- --- + | ID | | ID | | ID | | ... | | Category_ID | | Sub-category_ID | | | | ... | | ... | + ---------------- + + ---------------- + ------------- --- +
as its database structure, has_many: products ,: through = & gt; Subcategories work for model
model class
.
class category & lt; ActiveRecord :: Base has_many: subcategories have_many: products ,: through = & gt; : End of subcategories
subcategory. Rb class subcategory & lt; ActiveRecord :: Base is_to: Category has_many: products end
Product.rb class product & lt; ActiveRecord :: Base is_to: subcategory has_one: category ,: through = & gt; : Subcategory # does not need this, but this end of the work
ruby script \ console & gt; & Gt; C = class. Create = & gt; # & Lt; Category Id: 1, ... & gt; & Gt; & Gt; C.subcategories.create = & gt; # & Lt; Subcategory ID: 1, Category_ID: 1, ... & gt; & Gt; & Gt; P = s.products.create = & gt; # & Lt; Product ID: 1, Subcategory_ID: 1, ... & gt; & Gt; & Gt; C.products = & gt; [# & Lt; Product ID: 1, Subcategory_ID: 1, ... & gt;] & gt; & Gt; P.category # if you have_one allocence = & gt; # & Lt; Category Id: 1, ... & gt;
Comments
Post a Comment