how to reset all invalid fields of a mongoid document? - mongodb

I have a mongoid document (in a rails app) that has failed validation. I want to reset all of the invalid fields. Currently I am doing this:
#product.errors.each do |e,m|
method_name = "reset_#{e}!"
#product.send(method_name)
end
This is ok, but is there not a better (more concise) way to do this? I've read the dirty tracking documentation and googled, but I can't find anything about this.

I don't know what do you mean with 'more concise' but you don't need use "send"
#product.errors.errors.each do |f|
#product[f] = c.changes[f.to_s].first
end

Related

How do you create branching logic/skipping rules in a Microsoft Access form?

I'm creating a very simple Access database with a table and corresponding form. For some questions on the form, I'd like to disable following questions, or hide them using branching logic.
For example, in my form I have a combobox question that asks: Are you a smoker? - "Yes", "No", "Prefer not to answer". The following question is: If yes, how often do you smoke? If they chose the answers "No" or "Prefer not to answer" for the first question, then I don't want the second question to be visible/enabled.
I've been searching for a way to do this and the easiest way seems to be setting the Visible property of textbox "If yes, how often do you smoke?" to No. After that, I go to question "Are you a smoker?" and go to Event Procedure in the Properties menu. This brings up a VBA code editor with the following text:
Option Compare Database
Private Sub Text969_Click()
End Sub
Private Sub Combo367_Click()
End Sub
I've been looking at different pages but I can't seem to get the code to work. For the particular question I'm asking, the name of the form is "Chronic Smokers" and the field for the first question is named "Are you a smoker." and the second question is named "If yes, how often." This is the code I've been trying and it doesn't work, but I can't seem to figure anything else out:
Option Compare Database
Private Sub Text969_Click()
End Sub
Private Sub Combo367_Click()
If Chronic Smokers.Combo367='Yes' then
Chronic Smokers.If yes, how often.Visible = True
Else
Chronic Smokers.If yes, how often.Visible = False
End if
End Sub
I think part of my problem is that I don't know the way the naming conventions or syntax for this code works. I have a feeling part of the problem is that I have blank spaces without underscores in the names If anybody can help me out with this, I'd really appreciate it!
VBA code could be as simple as Me.[how often].Visible = Me.Combo367 = "Yes". No If Then Else needed. Code will need to be in combobox AfterUpdate as well as form Current events, not Click event. Code will apply to ALL instances of control, not just the current record.
NOTE: use of Me qualifier is shorthand for form/report object code is behind.
If you prefer to use If Then Else, correct syntax would be:
With Me
If .Combo367 = "Yes" Then
.[how often].Visible = True
Else
.[how often].Visible = False
End if
End With
Suggest you explore Conditional Formatting. It allows to enable/disable textboxes and comboboxes dynamically per record without VBA. Controls will still be visible but 'greyed out'.
And yes, strongly advise not to use spaces nor punctuation/special characters (underscore is only exception) in naming convention nor reserved words as names. If you do, then enclose in [ ] as shown above. Better naming would be ChronicSmokers and HowOften. And give objects more meaningful names than the defaults assigned by Access.

Auto complete in text field in odoo

I would like to select a customer from the select box by start typing its phone number.
How can I do that?
I have seen some are using name_search method.But still i am confused how to use it in both front end and back end.
Or is there any other solution for this.
Override the name_search method of your model and the domain you want on the args variable. Take a look at addons/account/account.py around line 595 args += [('type', '=', type)] for a concrete implementation. Make sure that you return the appropriate data structure as documented in the method's docstring at openerp/models.py.
For Auto complete in odoo. It provides suggestion only in case of using Many2one field in any module.
If you want to show suggestion and autocomplete. Create a model to store the mobile numbers and then use that particular model as foreign key in existing model.
That will do for you.

Web2py - Multiple tables read-only form

I've searched around the web for a way to achieve this, and found multiple solutions. Most of them had messy code, all of them drawbacks. Some ideas involved setting default values of all the db fields based on a record. Others worked by appending multiple SQLFORMs, which resulted in differences in indentation on the page (because it's 2 HTML tables in 1 form).
I'm looking for a compact and elegant way of providing a read-only representation of a record based on a join on two tables. Surely there must be some simple way to achieve this, right? The Web2py book only contains an example of an insert-form. It's this kind of neat solution I am looking for.
In the future I will probably need multi-table forms that provide update functionality as well, but for now I'll be happy if I can get a simple read-only form for a record.
I would greatly appreciate any suggestions.
This seems to work for me:
def test():
fields = [db.tableA[field] for field in db.tableA.keys() \
if type(db.tableA[field]) == type(db.tableA.some_field)]
fields += [db.tableB[field] for field in db.tableB.keys() \
if type(db.tableB[field]) == type(db.tableB.some_field)]
ff = []
for field in fields:
ff.append(Field(field.name, field.type))
form = SQLFORM.factory(*ff, readonly=True)
return dict(form=form)
You could add in field.required, field.requires validtaors, etc. And also, since you're using SQLFORM.factory, you should be able to validate it and to updates/inserts. Just make sure that the form you are building using this method contains all of the necessary information to validate the form for update -- I believe you can add them easily to the Field instantiation above.
EDIT: Oh yeah, and you need to get the values of the record in question to pre-populate the form based on a record id (after form is defined)... also.. I just realized that instead of those list comprehensions, you can just use SQLFORM.factory and provide the two tables:
def test():
form = SQLFORM.factory(db.tableA, db.tableB, readonly=True)
record = ... (query for your record, probably based on an id in request.args(0))
for field in record.keys():
if (*test if this really is a field*):
form.vars[field] = record[field]
return dict(form=form)
Some tweaking will be required since I only provided psuedo-code for the pre-population... but look at: http://web2py.com/books/default/chapter/29/7#Pre-populating-the-form and the SQLFORM/SQLFORM.factory sections.

TermQuery not returning on a known search term, but WildcardQuery does

Am hoping someone with enough insight into the inner workings of Lucene might be able to point me in the right direction =)
I'll skip most of the surrounding irellevant code, and cut right to the chase. I have a Lucene index, to which I am adding the following field to the index (variables replaced by their literal values):
document.Add( new Field("Typenummer", "E5CEB501A244410EB1FFC4761F79E7B7",
Field.Store.YES , Field.Index.UN_TOKENIZED));
Later, when I search my index (using other types of queries), I am able to verify that this field does indeed appear in my index - like when looping through all Fields returned by Document.GetFields()
Field: Typenummer, Value: E5CEB501A244410EB1FFC4761F79E7B7
So far so good :-)
Now the real problem is - why can I not use a TermQuery to search against this value and actually get a result.
This code produces 0 hits:
// Returns 0 hits
bq.Add( new TermQuery( new Term( "Typenummer",
"E5CEB501A244410EB1FFC4761F79E7B7" ) ), BooleanClause.Occur.MUST );
But if I switch this to a WildcardQuery (with no wildcards), I get the 1 hit I expect.
// returns the 1 hit I expect
bq.Add( new WildcardQuery( new Term( "Typenummer",
"E5CEB501A244410EB1FFC4761F79E7B7" ) ), BooleanClause.Occur.MUST );
I've checked field lengths, I've checked that I am using the same Analyzer and so on and I am still on square 1 as to why this is.
Can anyone point me in a direction I should be looking?
I finally figured out what was going on. I'm expanding the tags for this question as it, much to my surprise, actually turned out to be an issue with the CMS this particular problem exists in. In summary, the problem came down to this:
The field is stored UN_TOKENIZED, meaning Lucene will store it excactly "as-is"
The BooleanQuery I pasted snippets from gets sent to the Sitecore SearchManager inside a PreparedQuery wrapper
The behaviour I expected from this was, that my query (having already been prepared) would go - unaltered - to the Lucene API
Turns out I was wrong. It passes through a RewriteQuery method that copies my entire set of nested queries as-is, with one exception - all the Term arguments are passed through a LowercaseStrategy()
As I indexed an UPPERCASE Term (UN_TOKENIZED), and Sitecore changes my PreparedQuery to lowercase - 0 results are returned
Am not going to start an argument of whether this is "by design" or "by design flaw" implementation of the Lucene Wrapper API - I'll just note that rewriting my query when using the PreparedQuery overload is... to me... unexpected ;-)
Further teachings from this; storing the field as TOKENIZED will eliminate this problem too, as the StandardAnalyzer by default will lowercase all tokens.

How to get whole index in ZEND Lucene?

Hi I am looking for a way to get the whole index if my query is nothing.
My lucene is a picture of my database without some unwanted content, like expired annonces...
So I would like to use only lucene to get annonces, and for that I need a way to get the whole index.
Any ideas?
thanks!
This is not the answer but something wich works for me:
Using an indexKey like is_indexed, always true.
I am adding "is_indexed:1" to my query and it works...
If you have something else, let me know!
I tend to use a field which is named after the index such as:
$oDoc->addField(
Zend_Search_Lucene_Field::keyword(
'index',
'availability'
)
);
Then the term query will return all fields. It's not pretty but it works fine.