We have table which stores hash data key and value as below
sr_category_id_is"=>["194", "195", "196", "197", "198", "199"]}
We need to update the value using migration.
Sample data:
facets: {"status_x_days"=>"", "sr_category_id_is"=>["194", "195", "196", "197", "198", "199"]}
you cannot update hash directly in to your db.
You have to assign the existing hash to a variable, do the modifications on that variable, assign it back to the ActiveRecord object and update it.
Example.
Consider User address is a hash
user = User.first
address = user.address
address[:City] = "Ahmedabad" #Changed the city from Delhi to Ahmedabad
user.address = address
user.save
Related
I am having challenge on how to setup model for table object with arrays of responses in Sequelize ORM. I use Postgres DB. I have a table say foo. Foo has columns
A
B
C
> C1_SN
C1_Name
C1_Address
C1_Phone
D
E
The column C has a boolean question, if the user select true, he will need to provide array of responses for C1. Such as we now have:
C1_SN1
C1_Name1
C1_Address1
C1_Phone1
------
C1_SN2
C1_Name2
C1_Address2
C1_Phone2
-----
C1_SN3
C1_Name3
C1_Address3
C1_Phone3
I expect multiple teams to be filling this table. How do I setup the model in sequelize? I have two options in mind.
Option 1
The first option I think of is to create an extra 1:1 table between Foo and C1. But going with this option, I don't know how to bulkCreate the array of C1 responses in the C1 table.
Option 2
I think it's also possible to make C1 column in Foo table have a nested array of values. Such that if userA submit his data, it will have the nested array of C1. But I don't know how to go about this method as well.
You need to create separate table for C if user select true then need pass array of object and then pass in bulkCreate like.
C1_SN AutoIncrement
C1_NAME
C1_Address
C1_Phone
value=[{"C1_NAME":"HELLo","C1_Address":"HELLo","C1_Phone":"987456321"},{"C1_NAME":"HELLo1","C1_Address1":"HELLo","C1_Phone":"987456321s"}]
foo.bulkCreate(value).then(result=>{
console.log(result)
}).catch(error=>{
console.log(error)
})
From the official you can check this link:
Sequelize bulkCreate
i have two fields that should be related to res.partner
in partner_ids i want to choose partner and in recipients_ids i want to choose another partner that will get a copy of the document. the problem that in form view if i change partner_ids or recipient_ids both fields became the same. how can i do that i can choose different partners in those fields?
partners_ids = fields.Many2many('res.partner', string='Companys Names')
recipients_ids = fields.Many2many('res.partner', string='Copys for')
You are getting the error because the two field work on the same table in postgres
because odoo create a table for that name like this:
current_model_name_co_model_name_rel
in your case
your_model_res_partner_rel
so you need to tell odoo that every field has it's own relation
partners_ids = fields.Many2many('res.partner', # co_model
'your_model_partners_rel', # relation name change your_model to much your model name
string='Companys Names')
recipients_ids = fields.Many2many('res.partner',
'your_model_recipients_rel',
string='Copys for')
when you create m2m field it's better to specify this value by keyarguement
_name = 'my.model'
# exmple
user_ids = fields.Many2many(comodel_name='res.users', # name of the model
relation='my_model_users_rel', # name of relation in postgres
column1='session_id', # id reference to current mode
column2='user_id', # id reference to co_model
string='Allowed users')
Case in point
Suppose that I make a complex query a database from an application, either by plain SQL or by a an ORM library:
SELECT user.name, book.title, home.address
FROM user
JOIN book on user.book_id = book.id
JOIN home on book.home_id = home.id;
The result is a list of (name, title, address) tuples.
It is often convenient to initialize an object from each row, a-la:
def Shipping(object):
def __init__(self, row):
self.name = row[0]
self.title = row[1]
self.address = row[2]
def action(self):
return "Sending %s to %s at %s" % (self.book, self.user, self.address)
The question
Is there a design pattern for an object that represents a row in a DB query?
Notes
Following the Active Record answer: This case is different. An Active Record refers to a row in a table, and it is tied to the DB. This means that changes in the object can be materialized in the DB. My case discusses an object that reflects a query, which might be the result of a complicated JOIN. The resulting object is not related to the database, and changes in it can not be reflected back to it.
The Active Record pattern represents each database row with an object (although it gives that object the ability to update the database, which you didn't specify).
I want to change attributes of a lot of records via update_all.
The user should be able to change the prio relatively (record 1 has prio 0, record 2 prio 200, the user can choose to give them +200 so they end up with prio 200 and 400 respectively).
and change other columns at same time.
The preferred way in rails is
Model.update_all(name: name)
but for prio I have only the string form that I am aware of right now -
Model.update_all("prio = prio + #{change_of_prio}")
How can I change the latter into the Hash-Form of rails? Or, how can I do both in one update_all statement (without loosing the advantage of rails doing the necessary escaping when calling it with a Hash)?
You have to use array notation. This would work:
Model.update_all(["prio = prio + :change_of_prio, name = :name", {change_of_prio: 200, name: "foo"}])
# UPDATE `models` SET prio = prio + 200, name = 'foo'
:change_of_prio and :name are named placeholders referring to corresponding values in the hash. Rails handles the escaping for you.
The #update_all could be applied on enumerator to set or update all the record wot the specified values. The examples:
# Update all customers with the given attributes
Customer.update_all wants_email: true
# Update all books with 'Rails' in their title
Book.where('title LIKE ?', '%Rails%').update_all(author: 'David')
# Update all books that match conditions, but limit it to 5 ordered by date
Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(author: 'David')
So in your case the call will update all the records with the same value of the name variable, which is passed to the function as an argument.
Model.update_all(name: name) # all columnt named 'name' will have value of variable `name`
As for prio:
prio = ...
change_of_prio = ...
Model.update_all(prio: "#{prio} #{change_of_prio}") # all columnt named 'prio' will have value of variable concat `prio` and `change_of_prio`
I am new to rails so please anybody tell me how to use group_by option in controller page and and along with group_by i want to count the name through the Where Condition
In City Model add a scope.
scope :by_name, lambda { |name| where(name: name) }
When you call count on the scope
City.by_name('London').count
The following MySql will be executed...
SELECT count(*) FROMcitiesWHEREcities.name= 'London'
City.group_by(&:name)
The above statement will give you array of hash, in which key will be city_name and values will be array of city records.
Then if you need only count of each of array of city records for all the cities then you can do it by creating a new variable and storing the count of records along with their name using :
city_count = {}
City.group_by(&:name).each do |city_name, city_records|
city_count[city_name] = city_records.count
end
The above code will return you the array of hash which has key as city_name and the number of records as value.