DataMapper associates that depend on two columns - ruby

I have a table of products with attributes like SKU, Name, etc.
It seems like I need to have separate tables for each category of product due to the large variety of features depending on the category.
Say I have a table/class for boots, tools and hats
I'd like (unless there's a better way) to join/associate my products table to the other tables where appropriate (depending on category)?
Products Table
id | Name | SKU | Category(Table) | CategoryTableForeignKey
----------------------------------------------------------------------
1 | boot1 | 123 | Boots | 2
2 | knife1 | 345 | Tools | 42
-
Boots Table
id | product_id | Size | Width | Color | Sex
----------------------------------------------------------------------
2 | 1 | 9.5 | Wide | Olive | Male
-
Tools Table
id | product_id | length | Fixed | Coating | Etc
----------------------------------------------------------------------
42 | 2 | 4.5 | False | ... | ...
I'm not sure how to do this with DataMapper classes.
Ideally, I'd like to be able to:
my_boot = Boots.get(1)
my_boot.product.sku
Edit: dkubb pointed out a flaw in my code.

When you use has() what you're saying is that the foreign key is in the other model. If you want the foreign key to be in the table associated with the current model you want to use belongs_to.

class Product
include DataMapper::Resource
#descripe product properties ...
has n, :boots
end
Class Boot
include DataMapper::Resource
#describe boot properties ...
belongs_to :product
end
About DataMapper Associations: http://datamapper.org/docs/associations.html

Related

Multi user type design in Laravel

I'm a bit puzzled on how to best create an architecture for a multiple user type account. Watching the Laracasts episode on users and roles I figured a single User model with a Role model would be a good approach. However, rethinking the data I need for my different type of users varies, as for some users I need full address data and for others a name would be enough (to gently great them upon login). However, a Vistor might turn into a Customer. In fact, even the MerchantEmployee could become a Customer. This makes me wondering what my database design would become, e.g.
users => UserModel <fields> email, password
role: visitor => VistorModel <fields> name
role: customer => CustomerModel <fields> name, street, zipcode, ...
role: merchantowner => MerchantOwnerModel <fields> name, street, zipcode, ...
role: merchantemployee => MerchantEmployeeModel <fields> name
Would then all the Vistor, Customer, MerchantOwner and MerchantEmployee models have a user_id-column? Or how would you design such functionality?
I think to create two tables
First one is users
+----------+---------+-------+
| Col | Type | Notes |
+----------+---------+-------+
| id | bigInt | PK/AI |
| name | VARCHAR | NN |
| email | VARCHAR | NN|UN |
| password | VARCHAR | NN |
| role | ENUM | NN |
+----------+---------+-------+
The second one is user_data
+----------------------+---------+-------+
| Col | Type | Notes |
+----------------------+---------+-------+
| id | bigInt | PK/AI |
| user_id | bigInt | FK |
| address | VARCHAR | NN |
| {WHAT EVER YOU WANT) | | |
+----------------------+---------+-------+
And the relation to be one to one.
I recommend to use Laravel validation "required_if" to valid data before process it to database.
$this->validation($request, [
'address' => 'required_if:role,merchant_owner'
]);

Connect a table to many to many table

I have a many to many table from goods and companies and i want to connect it with a one to many table
company_good
+----------------+------------+
| company_ID | good_ID |
+----------------+------------+
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
+----------------+------------+
sales
+----------+----------------+--------------+----------+
| id | date | company_id | good_id |
+----------+----------------+--------------+----------+
| 1 | 2019-02-01 | 1 | 1 |
| 2 | 2019-02-01 | 2 | 1 |
| 2 | 2019-02-01 | 1 | 1 |
+----------+----------------+--------------+----------+
what i want to ask is are there any naming column on sales based on laravel rule? what i know is when a table posts want to connect it to table users the naming on table post should be user_id. but in this case how do i name it?
First off, you'll definitely want to use Eloquent for this.
The column names can be whatever you want them to be. When you set up a many to many model method you can define the related column names in there
Here's an extract from the Laravel documentation:
In addition to customizing the name of the joining table, you may also
customize the column names of the keys on the table by passing
additional arguments to the belongsToMany method. The third argument
is the foreign key name of the model on which you are defining the
relationship, while the fourth argument is the foreign key name of the
model that you are joining to:
return $this->belongsToMany('App\Role', 'role_user', 'user_id',
'role_id');
Read more at https://laravel.com/docs/5.8/eloquent-relationships#many-to-many
I suggest to add an id column to company_good table and name corresponding model as CompanyGood and instead of company_id and good_id in sales table use company_good_id column

compound attribute in ezpublish content class

I am designing a content class in ezpublish 5 where i need a filed like rating in different categories. I want to give content editor an interface in admin panel where he can choose a category to rate from dropdown and then text field to put rating point (1-5).
How to achieve this in ezpublish5?
The Matrix data/fieldtype sounds like it's close to what you need.
It allows you to set a table of values with a set number of columns. The limitation here is that each field is a free text entry for users.
| Review | Rating | Notes |
|-------------------|-------------|--------------------|
| Customer Service | 5 | Friendly & polite |
| Quality of Food | 4 | Tasty & Plentiful |
| ... | ... | ... |
There is a symfony bundle available for the Matrix fieldtype to allow access to the data in the Symfony stack: https://github.com/ezcommunity/EzMatrixFieldTypeBundle.
Alternatively there is the option to create your own data/fieldtype: http://share.ez.no/learn/ez-publish/creating-datatypes-in-ez-publish-4 & https://doc.ez.no/display/EZP/eZ+Publish+5+Field+Type+Tutorial

Rails ActiveRecord use join or includes instead of find_by_sql to get attributes of two tables

The following gets me the results I want but I am trying to figure out if I could have done this with a join or includes instead.
#items = Item.find_by_sql("SELECT *
FROM items_with_metadata
FULL OUTER JOIN items ON items.id = items_with_metadata.item_id")
The result should be that I get all attributes from both tables and the attributes are null wherever the items_with_metadata did not match an item in the items table.
ALSO, I do not have any associations between the two tables, the id of some items just happens to be in both tables
So for example if I have
items table with
id | name | active
------------------
123 | a | 0
456 | b | 1
and items_with_metadata has
color | usable | location | item_id
-----------------------------------
red | yes | north | 123
the result of the query will be
id | name | active | color | usable | location | item_id
--------------------------------------------------------
123 | a | 0 | red | yes | north | 123
456 | b | 1 | | | |
I was hoping there was a way to do this using ActiveRecord's joins or includes or any other ActiveRecord method that is not find_by_sql
How about:
Item.joins('FULL OUTER JOIN items_with_metadata ON items.id = items_with_metadata.item_id')
EDIT:
You could also use:
#items = Item.includes(:items_with_metadata)
This will return only Item models, but will also load all relevant ItemWithMetadata models to the memory which will make them available via:
#items.first.items_with_metadata
The last statement won't cause a DB query, but load the item metadata from the memory.

Category/Product Pageviews Magento

I run an ecommerce company which is made on Magento.
I need to find Category and Product wise page views and conversions for my website.
Google Analytics does not show me the Pageviews data for the said groups.
Is there a way to track the same in GA or some extension to do so.
We tried to use Jirafe, the data was right but it seems to run the cron every hour and would
at times use too many DB instances and thus eating into site bandwidth.
I've got a template that included a popular products widget for the homepage. While researching how to remove a product I didn't want in there, I stumbled up the table report_viewed_product_index which may or nay not be part of their template's code.
The people are "Templates Master" and the module which contains this code is "TM Highlight". The below query may not be perfectly exact, but at least corresponds to our "top 5" products widget.
<?
SELECT sfoi.sku, count(rvpi.product_id)
FROM
report_viewed_product_index rvpi
INNER JOIN
( SELECT DISTINCT sku, product_id FROM sales_flat_order_item ) as sfoi
ON sfoi.product_id=rvpi.product_id
GROUP BY rvpi.product_id
ORDER BY count(rvpi.product_id) DESC
LIMIT 0,10;
+------------+------------------------+
| sku | count(rvpi.product_id) |
+------------+------------------------+
| HM69.BLK | 11055 |
| FP-HUS | 3455 |
| 2VCOILS | 2829 |
| TB900 | 1920 |
| 460-1505 | 1396 |
| 2112-0061 | 1293 |
| JAMBNUTS | 1077 |
| VM520-110 | 1076 |
| TR200038PC | 1066 |
| 016-721 | 1008 |
+------------+------------------------+
?>

Resources