This is what I'd like to do:
I have this piece of code:
customer = Customer.find(:first, :conditions => {:siteId => params[:siteId], :customerCode => params[:id]})
If :customerCode is null, I'd like to use :temporaryCode instead. But I don't know how.
Thanks in advance.
customer = Customer.find_by_siteid_and_customercode params[:siteId], params[:id]
customer ||= Customer.find_by_siteid_and_temporarycode params[:siteId], params[:id]
making use of finders is most safer, cleaner
I'm pretty sure you want to use COALESCE inside the database:
customer = Customer.where(:siteId => params[:siteId])
.where('coalesce(customercode, temporarycode) = ?', params[:id])
.first
The SQL COALESCE function returns the first of its arguments that isn't NULL. For example:
coalesce(column1, column2)
gives you column1 if column1 isn't NULL and column2 if column1 is NULL.
If you're using Rails2 then something like this should work:
Customer.find(:first, :conditions => [
'siteid = ? and coalesce(customercode, temporarycode) = ?',
params[:siteId], params[:id]
])
Related
I am trying to do a simple or statement in a controller
This generates one set of trips that I am interested in displaying and is working fine.
#trips = Trip.where(:userid => #friends)
However, i would like to add another set of trips; trips whose userid == current_user.id
#trips = Trip.where(:userid => current_user.id)
Trying to combine these two i tried...
#trips = Trip.where(:conditions => ['userid= ? OR userid=?', #friends, current_user.id])
Any idea where the bust is? Thanks in advance.
Simply pass an array to get the equivalent of the SQL WHERE ... IN clause.
Trip.where(userid: [#friends, current_user.id])
See the ActiveRecord Rails Guide, 2.3.3 Subset Conditions
Is #friends an array? ActiveRecord will convert your where statement into a SQL IN clause when you generate it with the hash syntax. When you build your OR statement in raw SQL, you need to do the IN manually.
#trips = Trip.where('userid IN ? OR userid = ?', #friends, current_user.id)
Also, your column is called userid ? Typicaly it would be called user_id - is this a typo or do you just have an abnormal database?
I have a User class and a GroupUser class. I'm trying to do a search by name of the users. I tried following what I read on the joins, but I have something wrong. Also I need to change my name portion of the query to a like instead of an equals
Here is the query I had initially built.
#users = GroupUser.joins(:users).where(:group_id => params[:group_id]).where(:users => {:name => params[:q]})
Try this:
#users = User.where("name ilike ? and id in (select distinct user_id from groups_users where group_id = ?)", "%#{params[:q]}%", params[:group_id])
I'm using Ruby on Rails 2.3.8 and I would like to get all the #products that haven't been already added to the listed_products array.
For example, let's say I've got the following code:
listed_products = ['1', '2', '3', '4', '5']
#Then, I would like to do something like SELECT * FROM products where id not in
#(listed_products), and save the result in #products
I know the above SQL syntax won't work, but I just wanted to give you guys the idea of what I want to achieve.
Is there a "rails way" for doing this?
Thanks in advance!
Yes, you can do the following (Rails 2.3.x):
listed_products = [1,2,3,4,5]
Product.find(:all, :conditions => ["id NOT IN (?)", listed_products])
Or this in Rails 3.0.x:
listed_products = [1,2,3,4,5]
Product.where("id NOT IN (?)", listed_products)
Pan's answer is correct, but you can also use scopes in 2.3.8, which lets you chain with other scopes for the class:
class Product
...
named_scope :excluding_ids, lambda { |*ids|
if ids.count==0 then
{}
else
{:conditions => ["id NOT IN (?)",ids]}
end
}
...
end
Then you can chain with other scopes in your class. Say you have a scope named :active. Then you can do:
Products.active.excluding_ids(*listed_products).find :all, ... more conditions ...
and it would be independent of order of the scopes:
Products.excluding_ids(*listed_products).active.find :all, ..
The best I can think of is:
SELECT * FROM products where id not = '1' and id not = '2' (etc...)
Not what you're after I'm sure, but it may be the only way!
Hey guys another rails issue,
Currently have a collection that is line items for an invoicing system. I want to increment the count of the line items if I add in an item that already exists. At the moment I'm using an exists? query on the collection but it seems to return regardless of the key.
The foreign key I'm using is item_id, so I try to do invoice_items.exists?(:item_id => item.id)
This wasn't returning, so I changed it to invoice_items.find(:conditions => ["item_id == ?", item.id) and I get a return that I cannot search without invoiceItem ID.
Ideas?
conditions => ["item_id == ?", item.id
should be
conditions => ["item_id = ?", item.id]
So your query look like this
invoice_items.find(:all).conditions => ["item_id = ?", item.id]
you should just need to do either
invoice_items.all(:conditions => ["item_id == ?", item.id])
OR
invoice_items.first(:conditions => ["item_id == ?", item.id])
and you can use the syntax
invoice_items.all(:conditions => {:item_id => item.id})
if you are going to use the model.find command the first parameter needs to be :all, :first, :last or the primary key you are searching for. that is why I generally prefer to use Model.find only if I am searching for an id, otherwise I use Model.first, Model.last, Model.all. That way you know what you are going to get.
I am stuck and I guess it is a syntax thing I just don't get:
It's a one(category)_to_many(product) relationship.
In my category model I have the columns pick1, pick2, pick3, pick4, pick5.
Each of those holds the id of a product.
In my category_controller I want to retrieve those in a find:
#productpicks = Product.find(:all, :conditions =>
['online = ? and category_id IN (?)', true,
[#category.pick1, #category.pick2, #category.pick3, #category.pick4, #category.pick5]])
... and iterate over them in the view like this:
do something
But there is nothing to be found in that array ...
Does anyone have an idea what I am doing wrong?
Thanks for helping!
Val
Shouldn't it be:
#productpicks = Product.find(
:all,
:conditions => [
'online = ? and id IN (?)',
true, [
#category.pick1,
#category.pick2,
#category.pick3,
#category.pick4,
#category.pick5
]
]
)
Replacing category_id with id in the where clause?
As pick1-5 hold product ids, and you are trying to find those specific products.