How to use lamda to get not equals value on a scope in ruby on rails? - ruby-on-rails-5

I have a scope thats not currently working.
scope :not_voided, lambda { where('workflow_state != ?', "voided") }
I want to get all orders that don't have a workflow_state of "voided"
I'm really struggling with it and any help would be appreciated.

This code actually works as expected.

Related

Where clause using LIKE condition in MVC4 lambda

I want to show results where w.DrawDate contains (LIKE %year%) the year.
return View(db.Euro.Select(p => p).Where(w => w.DrawDate == year).ToList());
As seen in Like condition in MVC4 using lambda, i should use the Contains() method, but it isn't available.
So, how can i make w.DrawDate == year into w.DrawDate LIKE %year%?
You already have the answer:
View(db.Euro.Where(w => w.DrawDate.Contains(year)).ToList());
And you're probably just missing a namespace

How do I get the results of the Plucky Query inside my controller?

I'm missing something simple - I do not want to access the results of this query in a view.
Here is the query:
#adm = Admin.where({:id => {"$ne" => params[:id].to_s},:email => params[:email]})
And of course when you inspect you get:
#adm is #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery:0x007fb4be99acd0>
I understand (from asking the MM guys) why this is the case - they wished to delay the results of the actual query as long as possible, and only get a representation of the query object until we render (in a view!).
But what I'm trying to ascertain in my code is IF one of my params matches or doesn't match the result of my query in the controller so I can either return an error message or proceed.
Normally in a view I'm going to do:
#adm.id
To get the BSON out of this. When you try this on the Decorated Query of course it fails:
NoMethodError (undefined method `id' for #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery:0x007fb4b9e9f118>)
This is because it's not actually a Ruby Object yet, it's still the query proxy.
Now I'm fundamentally missing something because I never read a "getting started with Ruby" guide - I just smashed my way in here and learned through brute-force. So, what method do I call to get the results of the Plucky Query?
The field #adm is set to a query as you've seen. So, to access the results, you'll need to trigger execution of the query. There are a variety of activation methods you can call, including all, first, and last. There's a little documentation here.
In this case, you could do something like:
adm_query = Admin.where({:id => {"$ne" => params[:id].to_s},:email => params[:email]})
#adm_user = adm_query.first
That would return you the first user and after checking for nil
if #adm_user.nil?
# do something if no results were found
end
You could also limit the query results:
adm_query = Admin.where( ... your query ...).limit(1)

Querying attributes where value is not yet set

Any of you guys know how to query rally for a set of things where an string attribute value is currently not yet set?
I can’t query for the value equal to an empty string. That doesn’t parse. And I can’t use “null” either. Or rather, I can try “null” and it parses fine but it doesn’t result in finding anything.
query = #rally_api.find(:defect, :fetch =>true,
:project_scope_up => false, :project_scope_down => false,
:workspace => #workspace,
:project => #project) { equals :integration_i_d, "" }
This was followed up by telling the me to substitute "" with nil which didn't work. Null was tried to no success as well. I've tried "null" and null and "". None of them work.
I'm not familiar with our Ruby REST toolkit but directly hitting our WSAPI, you would say (<Field> = null). Notice that there are no quotes around "null".
Also, I'm wondering if the use of contains in your example above is what you wanted. You might want =.
try: { equal :integration_i_d, '""' }
Also, if rally_rest_api seems slow - we're working on something faster here:
http://developer.rallydev.com/help/ruby-rally-api

Rails 2-cases scope

I have scope
scope :for_list, lambda { |brand_ids|
where('brands.id IN (?)', brand_ids).includes(:models).where("models.popular = '1'").order('models.name')
}
But some times there are no models.popular = 1
And in this case I want to select all from models ignoring popular parameter
How to write that scope?
Since it's simply a lambda function, you can use an if/else in there and it'll get evaluated at the time it's called. Therefore, you could write the scope like this:
scope :for_list, lambda { |brand_ids|
if self.popular > 1
where('brands.id IN (?)', brand_ids).includes(:models).where("models.popular = '1'").order('models.name')
else
where('brands.id IN (?)', brand_ids).includes(:models).order('models.name')
end
}
That should work. However, I'd caution against using this. That logic doesn't belong in a scope. Instead, you should question you your views and controllers are set up and rework those.

Rails scope that does nothing for NOT IN values

I have a Rails 3 scope that excludes an array of ids.
What is the best way to write the scope so that it does nothing when the array is empty and is still chainable? I currently have this, which works, but seems a little hokey:
scope :excluding_ids,
lambda {|ids| ids.empty? ? relation : where('id not in (?)', ids) }
If I do not have the "ids.empty? ? relation :" bit, when ids is empty the SQL generated is
... ID not in (NULL) ...
which will always return nothing. So something like:
Model.excluding_ids([]).where('id > 0')
returns no results.
If the ids array is empty then don't return anything.
scope :excluding_ids, lambda { |ids|
where(['id NOT IN (?)', ids]) if ids.any?
}
The query will run without any additional constraints on the query if there are no ids.
In Rails 4 you can use:
scope :excluding_ids, ->(ids) { where.not(id: ids) }
Here's a slight variation on Douglas' answer, using ruby 1.9 stabby lambda syntax and without the brackets in the where method.
scope :excluding_ids, ->(ids) {where("id NOT IN (?)", ids) if ids.any?}
How about the following? (It still checks for an empty array though, so if that's what you're trying to avoid it's not much of an improvement :)
scope :excluding_ids,
lambda {|ids| (ids.empty? && relation) || where('id not in (?)', ids) }