Rails, PostgreSQL - how to count the number of tags? - ruby

I have these DB tables:
taggings
id | integer | not null default nextval('taggings_id_seq'::regclass) | plain |
tag_id | integer | | plain |
taggable_id | integer | | plain |
taggable_type | character varying(255) | | extended |
tagger_id | integer | | plain |
tagger_type | character varying(255) | | extended |
context | character varying(128) | | extended |
created_at | timestamp without time zone | | plain |
tags
id | integer | not null default nextval('tags_id_seq'::regclass) | plain |
name | character varying(255) | | extended |
These tables are created by the acts-as-taggable-on gem.
I am trying to get the count of most used tags that are stored in the table taggings, but I have no idea how to get them...
Could anyone give me some tip or link, how to get that?

Related

Laravel get value from db with columns key/value

I have a table on my DB with these columnns:
+-----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| value | varchar(255) | NO | | NULL | |
+-----------------+---------------------+------+-----+---------+----------------+
I would access to value values by name.
For example I have these datas:
+----+------------+------------+
| id | name | value |
+----+------------+------------+
| 1 | start_date | 2020-01-01 |
| 2 | end_date | 2021-01-01 |
+----+------------+------------+
I would to get '2020-01-01' by 'start_date'.
I tried this code, but I'm not satisfyed because with this code I get all values of the row, not only the value expected.
Configuration::get()->keyBy('start_date');
I'm not sure I was clear.
Let me know.
Thanks a lot!!
Assuming you want to get an array of key/value pairs, and each key in the name column is unique, you can simply use pluck() (https://laravel.com/docs/8.x/collections#method-pluck):
$configuration = Configuration::pluck('value', 'name');
dd($configuration);
// ['start_date' => '2020-01-01', 'end_date' => '2021-01-01']
Then, you'd use simply array access to use these configuration settings where applicable:
$startDate = $configuration['start_date']; // '2020-01-01'
$endDate = $configuration['end_date']; // '2021-01-01'
...

Return filtered records from a returned set of data from two tables

I have three tables:
- Venue
- Space (belongs to Venue)
- Included Space (belongs to Space)
I receive the id of a Venue in the route and return all the related spaces that I know have Included Spaces(a field called num_included_spaces__c on the Space record that maintains a count of its children). Now that I have all the related parent Spaces for that Venue, I need to find all of the Included Spaces for them.
An Included Space is still a Space, it just happens to have a parent that resides in the same table. I'm trying to turn this:
Venue = Rockdog
- Space = Upstairs
- Space = Media Room
- Space = Courtyard
- Space = Downstairs
- Space = Front Patio
- Space = Indoor Bar
Into this:
Venue = Rockdog
- Space = Upstairs
-- Included Space = Media Room
-- Included Space = Courtyard
- Space = Downstairs
-- Included Space = Front Patio
-- Included Space = Indoor Bar
The Included Spaces table has belongs_to__c and space__c as fields, where belongs_to__c is the id of the parent space and space__c is the id of the child. So i'm looking to find all the Included Spaces where belongs_to_c matches the id of any #spaces returned below
#sub_spaces = Space.where("venue__c = ? AND num_included_spaces__c = ?", params[:venue],0)
#spaces = Space.where("venue__c = ? AND num_included_spaces__c > ?", params[:venue],0)
How would I write this Active Record Query for #included_spaces?
my database schema.
mysql> describe venues;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0,00 sec)
mysql> describe spaces;;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| venue_id | int(11) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0,00 sec)
ERROR:
No query specified
mysql> describe included_spaces;;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| space_id | int(11) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0,00 sec)
ERROR:
No query specified
Below function will somehow give you the result you need (in console ofcourse ) however it's not a good solution - it queries database more than needed. However it is the easy -))
def foo id
v = Venue.find(id)
puts v.name
v.spaces.each do |space|
puts space.name
space.included_spaces.each do |spis|
puts spis.name
end
end
end
You can also try a more complex query sth like,
mysql> SELECT spaces.name, included_spaces.name FROM `spaces` INNER JOIN `venues` ON `venues`.`id` = `spaces`.`venue_id` INNER JOIN `included_spaces` ON `included_spaces`.`space_id` = `spaces`.`id` WHERE `spaces`.`venue_id` = 1
-> ;
+------------+-----------+
| name | name |
+------------+-----------+
| Upstairs, | Front |
| Upstairs, | Patio, |
| Upstairs, | Indoor |
| Upstairs, | Bar |
| Downstairs | Media |
| Downstairs | Room, |
| Downstairs | Courtyard |
+------------+-----------+
7 rows in set (0,00 sec)
which should be translated to active record as
Space.joins(:venue)
.joins(:included_spaces)
.where(venue_id: 1)
.select('spaces.name, included_spaces.name')

Sphinx malformed table

We've got a RST table in sphinx that looks like this:
+-------------------------+---------------------------------------------+-----------------------------------------+
| Key | Appearance in the administration | Value |
+=========================+=============================================+=========================================+
| |text_line| | simple text input | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |text_area| | text area | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |text_editor| | text editor with formatting capabilities | HTML string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |checkbox| | checkbox | boolean |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |single_select| | list of radio buttons | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |multiple_select| | list of checkboxes | array of strings |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |color| | color picker | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |date| | date picker | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |time| | text input with time validation | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |url| | text input with URL validation | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |email| | text input with email validation | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |password| | password input | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |phone| | text input for a phone number | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |internal_links| | widget for selecting links to other pages | resolved pages as defined in parameters |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |single_internal_link| | widget for selecting a single page | resolved page as defined in parameters |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |smart_content| | widget for configuring a data source | resolved pages as defined in parameters |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |resource_locator| | widget for entering the URL of a page | string |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |tag_list| | autocomplete input for entering and adding | array of strings |
| | tags | |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |category_list| | autocomplete input for entering and adding | array of strings |
| | tags | |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |media_selection| | widget for selecting media (images, | array containing arrays with |
| | documents) | urls for every format |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |contact_selection| | widget for selecting contacts | array containing array representations | |
| | | of the contact objects |
+-------------------------+---------------------------------------------+-----------------------------------------+
| |snippet| | widget for selecting snippets | array containing array representations |
| | | of the snippets |
+-------------------------+---------------------------------------------+-----------------------------------------+
Since recently I get the following error when trying to build the documentation using make build:
ERROR: Malformed table.
It also outputs this table again, but without any hint what is wrong here, and I can't find it. Can anybody help me out?
While trying to format the code correctly I found the error on my own... The row for contact_selection contained a pipe at the very end of the line, after some tabs...

Action Controller: incompatible character encodings: ASCII-8BIT and UTF-8?

I have a text file that has some ® (Registered Trade Mark) symbols in it. The file is in UTF-8.
I'm trying to import this file and populate a MySQL database using Rails 3. The DB appears to be setup fine to take UTF-8
+-------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| user_id | int(11) | NULL | YES | MUL | NULL | | select,insert,update,references | |
| title | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| translation | text | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| created_at | datetime | NULL | NO | | NULL | | select,insert,update,references | |
| updated_at | datetime | NULL | NO | | NULL | | select,insert,update,references | |
+-------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
Yet, when I try to do this:
trans_file = params[:descriptions] #coming from file_field_tag
trans = trans_file.read.split("\r\n")
trans.each do |tran|
ttl = ''
desc = ''
tran.split(']=').each do |title|
if title =~ /\[/ #it's the title
ttl = title.sub('[','')
else
desc = title.gsub('FFF', "\r\n")
end
end
#current_user.cd_translations.build(title: ttl, translation: desc).save
I'm getting the error "Action Controller: incompatible character encodings: ASCII-8BIT and UTF-8".
I'm using the utf-8 encoding in my application.rb file, and I'm using the mysql2 gem.
If I remove the Registered Trade Mark character, the error goes away. But it's not really an option to strip it out of the incoming text.
I tried the solution here: https://stackoverflow.com/a/5215676/102372, but that didn't make any difference.
Stack trace:
app/controllers/users_controller.rb:28:in `block in update_cd_translations'
app/controllers/users_controller.rb:15:in `each'
app/controllers/users_controller.rb:15:in `update_cd_translations'
config/initializers/quiet_assets.rb:7:in `call_with_quiet_assets'
How can I resolve this?
It appears that ruby thinks that the encoding of the uploaded file is ascii-8bit (that is to say binary).
If you know the encoding of the file, you can use force_encoding! to change the encoding of the string (without transcoding). If you're not always going to be sure of the encoding of the file, the charguess gem can be used to guess it.
Try to add a
# -*- encoding : utf-8 -*-
at the beginnig of each file, which is included in the whole process

magento database structure?

Currently am working with magento project..
in which i have stucked on the point ..
that is when admin add any subcategory/category
by
Manage category -> custome design
here its gives two option like
Active from and Active to..
can anyone, who know about magento database, tell me where or in which table this two value store
Thanks for any suggestion or help!
I've listed the attributes for catalog categories below. Since categories are an EAV type, you'll need to look in a particular subtable to get your values. In this case, custom_design_from and custom_design_to are datetime values, and the name of your entity is catalog_category_entity, so the table you want is catalog_category_entity_datetime.
Next problem you'll find is getting the right attribute ID. Since they're liable to change, here's the SQL query to run in order to grab them:
select attribute_id, attribute_code from eav_attribute where entity_type_id = 3 and attribute_code in ('custom_design_from', 'custom_design_to');
I get 52 and 53, but YMWV. Hope that helps!
Thanks,
Joe
+----------------------+--------------+
| attribute_code | backend_type |
+----------------------+--------------+
| name | varchar |
| is_active | int |
| url_key | varchar |
| description | text |
| image | varchar |
| meta_title | varchar |
| meta_keywords | text |
| meta_description | text |
| display_mode | varchar |
| landing_page | int |
| is_anchor | int |
| path | static |
| position | static |
| all_children | text |
| path_in_store | text |
| children | text |
| url_path | varchar |
| custom_design | varchar |
| custom_design_apply | int |
| custom_design_from | datetime |
| custom_design_to | datetime |
| page_layout | varchar |
| custom_layout_update | text |
| level | static |
| children_count | static |
| available_sort_by | text |
| default_sort_by | varchar |
| include_in_menu | int |
+----------------------+--------------+
Active from is an attribute whose attribute_code is custom_design_from(attribute_id 57) and Active To is an attribute whose attribute_code(attribute_id 58) is custom_design_to.
This both attributes value are stored in database table `catalog_category_entity_datetime`.
Check above table with row like value of entity_id is your category id, attribute_id is 57 and active from value is store in value field of table same active to value is stored in value field with entity_id is your category id, attribute_id is 58.

Resources