I have a user model. I want to place a limitation of the rate of which the user can change his/her name.
Obivously a person doesn't change a name every day or every week.
How would be best to implement such a limit for example able to change once a month?
The only way I can think of is placing another attributes for each existing attribute so for the :name I will created :name_last_changed_at and each time I would test it.
Is there a more elegant/rails-way to do it?
You can relate on updated_at column and validate it in model.
Related
I need a new field inside Contact model that would hold information about Allowed companies of the related user.
Now there is only field about Currently picked company by that user (and it is not enough for me to make a record rule).
The field I want to copy values from is inside model Users and it is called company_ids.
I’m trying to add this field in the developer mode (Settings > Technical > Fields) like this:
But I’m having trouble with code that would fill my field with values from the another model.
for record in self:
record[("x_company_ids")] = env['res.users'].company_ids
I’m guessing that the record is referring to a record inside Contact model and it does not contain fields from another models like Users. So I can’t figure it out how to reference a field from another model.
Something similar to this: env['res.users'].company_ids?
It is even harder for me because it is many2many field and should always update when the source changes.
Maybe better solution would be to use Automatic action to write values to this field?
I saw some threads like this: Computed many2many field dependencies in Odoo 10.
But it seems like in those cases they had clear connection between the fields and I don't have it. I don't know how to get related user while I'm inside Contact model. I know only how to this oposite way (from user to contact): user.partner_id.id
Here in below given code you haven't specified related user from which you will get company_ids, you have directly accessing company_ids
for record in self:
record[("x_company_ids")] = env['res.users'].company_ids
You can write as following :
for record in self:
record["x_company_ids"] = self.env['res.users'].search([('partner_id','=',record.id)]).company_ids
Ok so in my rails project I have these models: pools, memberships, and week_scores
Pool has_many :memberships
Membership has_many :week_scores
WeekScore also has an attribute week:integer that tells what week it is. That way I know if that table is of week 1 or week 6, etc.
WeekScore has an attribute score:integer where I save the points of each user. This makes me think I should change my model name to week_points...
Anyways, I want to show a table of all me the members of a pool sorted by the score they got in a certain week_score.
Just to clarify
The way I get to a ceratin week score right now is
member_score = member.week_scores.find_by(week: 5).score
//member is a member of a pool
For example,
Lets say its week 5,
I want to display the week 5 score of each member of the pool sorted by the score in descending order like so,
David.... 30pts
John..... 28pts
Josh..... 28pts
Mike..... 21pts
...so on
what would be the query i need to achieve this? Ive tried joins but it wont work but im new at this so im pretty sure this is easy to do Im just too newb to know.
Also.. I want to read more about databases and sql for rails so I get more familiar with this. Anyone care to recommend a book?
week5 = Pool.first.memberships.includes(:week_scores)
.where(:week_scores => {week: 5}).order('week_scores.score DESC')
week5.each do |membership|
puts "#{membership.member_name}: #{membership.week_scores.first.score}"
end
I am trying to get a snapshot of deleted userstory to get value for a custom field(c_Dep). I get the snapshot but the custom field is empty. It had value in it. Does lookback not save value for cutomer created cutom field?
findConfig: {
_TypeHierarchy: 'HierarchicalRequirement',
"ObjectID": 12345,
"_ValidFrom": {
"$lte": "2017-01-25T19:00:57.475Z"
}
Sarita, It is hard to tell from the information you have given what is going on precisely. However, I can give you some pointers
The Lookback API will store changes in values for custom fields. The selection you have shown is valid from 24thJan to 25thJan. During this period was the custom field set? Probably not, because the array is only one long and I think it is showing the creation event.
Was the custom field updated to contain something after this time period?
The reason for asking is that a common misunderstanding is that the records stored in the lookback database will hold the current value of fields - it doesn't. It holds the changes in fields. If c_Dependencies didn't change during that time period, you may not see an entry returned in the array. The next entry in the database might be the record where the c_Dependencies field was set (changed from null to something) and that might be 'after' your time period filter.
It looks like your query is requesting snapshots earlier than 2017/1/25 ($lte). Since there's only one, it's probably the creation snapshot. If you get all snapshots for the ObjectID by removing the _ValidFrom parameter, you should see the changes made to c_Dep after artifact creation.
As I am not allowed to comment, I have to post a new answer.
I think William Scott meant remove the ValidTo filter. The one you have is the creation change. The update will be afterwards.
I have an orde field on one of my models, that model also has a is_milestone? method which returns true or false depending on whether the order is either a 50th or 100th milestone.
I wish to select all the items that are milestones but can this be done by using the is_milestone? method?
I currently have:
#milestones = Model.where(:is_milestone?).order('order ASC')
But it doesn't work and I think I have the syntax wrong but unsure how it should be written.
Any thoughts?
No - You cannot ask the database to do a query which requires interaction with your Ruby code.
The only way you'd be able to get this to work is to create an is_milestone attribute on your model and store the boolean value within the database. You could perhaps populate this value with an after_save callback which counted the milestones and set the value to true if it was either the 50th or 100th one.
I have a form for submitting an order, and I need an autocomplete field that searches across three attributes in the associated customer model: first name, last name, and customer_number (as opposed to customer.id). I know about the rails3-jquery-autocomplete gem found here http://github.com/crowdint/rails3-jquery-autocomplete, and got it working well, but a question has occurred to me -- is there a more efficient way to make the autocomplete work without having to query the db every time?
The other solution that occurred to me is to create a new indexed attribute in the customer model -- call it autocomplete_data. Whenever a new customer is added via the usual new customer form, an :after_create callback could populate the field. Would this speed up the performance? Or am I overthinking it?
UPDATE
I'm embarassed to say that I just didn't search hard enough the first time around -- I think this actually answers my question:
Rails: Efficiently searching by both firstname and surname