I am new in ROR
i want to set condition parameter with find attribute.
See below my code
#navmenu = MenuItem.find(:all,:conditions=>[MenuItem.menu_item_id == nil OR MenuItem.id != 1 AND MenuItem.is_active == 1] )
this query given error. Please help
Hope this will helps you:
#navmenu = MenuItem.find(:all,:conditions=>["menu_items.menu_item_id = ? OR menu_items.id <> ? AND menu_items.is_active = ?", nil, 1, true])
Thanks.
Related
I am modifying the SalesConfirmDP class and trying to add the CustVendExternalItem.ExternalItemTxt field into a new field I have created.
I have tried a couple of things but I do not think my syntax was correct i.e I declare the CustVendExternalItem table in the class declaration. But then when I try to insert CustVendExternalItem.ExternalItemTxt into my new field, it does not populate, I guess there must be a method which I need to include?
If anyone has any suggestion it would be highly appreciated.
Thank you in advance.
private void setSalesConfirmDetailsTmp(NoYes _confirmTransOrTaxTrans)
{
DocuRefSearch docuRefSearch;
// Body
salesConfirmTmp.JournalRecId = custConfirmJour.RecId;
if(_confirmTransOrTaxTrans == NoYes::Yes)
{
if (printLineHeader)
{
salesConfirmTmp.LineHeader = custConfirmTrans.LineHeader;
}
else
{
salesConfirmTmp.LineHeader = '';
}
salesConfirmTmp.ItemId = this.itemId();
salesConfirmTmp.Name = custConfirmTrans.Name;
salesConfirmTmp.Qty = custConfirmTrans.Qty;
salesConfirmTmp.SalesUnitTxt = custConfirmTrans.salesUnitTxt();
salesConfirmTmp.SalesPrice = custConfirmTrans.SalesPrice;
salesConfirmTmp.DlvDate = custConfirmTrans.DlvDate;
salesConfirmTmp.DiscPercent = custConfirmTrans.DiscPercent;
salesConfirmTmp.DiscAmount = custConfirmTrans.DiscAmount;
salesConfirmTmp.LineAmount = custConfirmTrans.LineAmount;
salesConfirmTmp.CurrencyCode = custConfirmJour.CurrencyCode;
salesConfirmTmp.PrintCode = custConfirmTrans.TaxWriteCode;
if (pdsCWEnabled)
{
salesConfirmTmp.PdsCWUnitId = custConfirmTrans.pdsCWUnitId();
salesConfirmTmp.PdsCWQty = custConfirmTrans.PdsCWQty;
}
**salesConfirmTmp.ExternalItemText = CustVendExternalItem.ExternalItemTxt;**
if ((custFormletterDocument.DocuOnConfirm == DocuOnFormular::Line)
|| (custFormletterDocument.DocuOnConfirm == DocuOnFormular::All))
{
docuRefSearch = DocuRefSearch::newTypeIdAndRestriction(custConfirmTrans,
custFormletterDocument.DocuTypeConfirm,
DocuRestriction::External);
salesConfirmTmp.Notes = Docu::concatDocuRefNotes(docuRefSearch);
}
salesConfirmTmp.InventDimPrint = this.printDimHistory();
Well, AX cannot guess which record you need, there is a helper class CustVendExternalItemDescription to deal with it:
boolean found;
str externalItemId;
...
[found, externalItemId, salesConfirmTmp.ExternalItemText] = CustVendExternalItemDescription::findExternalItemDescription(
ModuleCustVend::Cust,
custConfirmTrans.ItemId,
custConfirmTrans.inventDim(),
custConfirmJour.OrderAccount,
CustTable::find(custConfirmJour.OrderAccount).CustItemGroupId);
The findExternalItemDescription method returns more information than you need here, but you have to define variables to store it anyway.
Well, the steps to solve this problem are fairly easy and i will try to give you a step by step approach how to solve this problem.
1) Are you initialising CustVendExternalItem properly? Make a record of the same and initialise it as Jan has shown above, then debug your code and see if the value is being initialised in your DP class.
2)If your value is being initialised correctly, but it is not showing up in the report design there can be multiple issues such as:
Overlapping of text boxes.
Insufficient space for the given field
Some report parameter/property not being set correctly which causes
your value not to show up on the report.
Check these one by one and you should end up arriving towards a solution
Here is my code
if(!$criteria){
return null;
}
$em = $this->doctrine->getEntityManagerForClass('Sample\MyBundle\Entity\Call');
$qb = $em->createQueryBuilder();
$qb->add('select', 'c')->add('from', 'Sample\MyBundle\Entity\Call c');
if($criteria->getReason() && $criteria->getReason() != null){
$qb->add('where', 'c.reason = ?1');
$qb->setParameter(1, $criteria->getReason());
}
if($criteria->getCallDate() && $criteria->getCallDate() != null){
$qb->add('where', 'c.callTime = ?2');
$qb->setParameter(2, $criteria->getCallDate());
}
if($criteria->getPage()>1){
$qb->setFirstResult(($criteria->getPage()-1) * 10)->setMaxResults(10);
}
$query = $qb->getQuery();
return $query->getResult();
I'm getting this error - Invalid parameter number: number of bound variables does not match number of tokens I googled but couldn't found a solution. Need help with this.
I even tried to put both setParameter() calls after conditional check if statements but that also give the same error. If I don't set the second parameter and remove second where condition then it works.
Oh, I guess I was making a mistake. I think I can not use multiple 'where' in 'add' function. I replaced it with $qb->andWhere('c.callTime > ?2'); and now it works fine.
Sorry in advance for this incredibly simple question, but what is the best way to set a variable while also checking a condition. For example, I have:
#friends = []
#user.facebook_friends.each do |key,value|
if test = Authorization.includes(:user).find_by_uid(key) != nil
#friends << {"name" => test.user.name, "facebook_image_url" => test.user.facebook_image_url}
end
end
I am trying to pull in the user records when I pull in the authorization record, so as to minimize my database queries. I know that I can't write
test = Authorization.includes(:user).find_by_uid(key) != nil
in order to set the test variable. What is the best way to write this code so that it is functional?
You just need parens:
(test = Authorization.includes(:user).find_by_uid(key)) != nil
Also here is a more rails way to do it:
unless (test = Authorization.includes(:user).find_by_uid(key)).nil?
#friends << {"name" => test.user.name, "facebook_image_url" => test.user.facebook_image_url}
end
Currently, I've got this:
self.profile_pic = user_info['image']
self.birthday = user_extra['birthday']
self.city = user_extra['location']['name']
Sometimes the user_extra or user_info var is blank. I would like to have a default value then. How to do this?
Thanks in advance,
Maurice
It really depends on how specific you want to get. Is it blank or nil? Because there's a big difference there. In Ruby, a blank String is not evaluated to false. Assuming that the specific hash of that variable is nil, this would work...
self.profile_pic = user_info['image'] || "default"
self.birthday = user_extra['birthday'] || "default"
self.city = user_extra['location']['name'] || "default"
Or you could have it be a more general check to make sure the variable isn't nil itself, using the ternary operator...
self.profile_pic = user_info ? user_info['image'] : "default"
self.birthday = user_extra ? user_extra['birthday'] : "default"
self.city = user_extra ? user_extra['location']['name'] : "default"
Or, if it actually is blank and not nil, something more like this...
self.profile_pic = user_info.empty? ? "default" : user_info['image']
self.birthday = user_extra.empty? ? "default" : user_extra['birthday']
self.city = user_extra.empty? ? "default" : user_extra['location']['name']
It depends on the exact conditions you have, and how far down the rabbit hole you want to go. :) Hope that helps!
I have this piece of code, that checks whether a returned object is null. If so, it will return 0, or else it will return a property inside the object.
var userPoints = (from Point p in entities.Point
where p.UserName == userName
select p).SingleOrDefault();
if (userPoints == null)
{
return 0;
}
else
{
return userPoints.Points;
}
Is it possible to simplify the if statements with the nullable operator? I've tried this, but the system throws an exception when attempting to read the property
return userPoints.Points ?? 0;
No, unfortunately there's nothing which will do exactly that. Options:
Use the conditional operator:
return userPoints == null ? 0 : userPoints.Points;
Change your query to make that do the defaulting:
return (from Point p in entities.Point
where p.UserName == userName
select p.Points).SingleOrDefault();
Personally I'd go for the latter approach :) If you wanted a default other than 0, you'd need something like:
return (from Point p in entities.Point
where p.UserName == userName
select (int?) p.Points).SingleOrDefault() ?? -1;
You can do this:
var userPoints = (from Point p in entities.Point
where p.UserName == userName
select p.Point).SingleOrDefault();
return userPoints;
If there are no results then userPoints will be 0, otherwise it will be the value of Points.
You can't use it in your context.
Explanation:
You want to check whether userPoints is null, but want to return userPoints.Points if it is not null.
The ?? operator checks the first operand for null and returns it, if it is not null. It doesn't work if what you want to check and what you want to return are two different things.
Hover over var with the mouse and see what type it is.