I have a user table in that department_id is one of the field and i have to check two condition where :department_id=>current_user.id or department_id=> null.
I do not know how to write query for this in rails3.
Here is an example assuming you have a Customer model.
Customer.where("department_id = ? OR department_id IS NULL", current_user.id)
You may also check the guides here:
http://guides.rubyonrails.org/active_record_querying.html#conditions
Related
I'll try my best to explain the situation here. We are using Maria DB.
In certain case we are looping over all the records/rows of a REQUESTS table.
and for each object/record we are checking few conditions and if condition passes we are performing an action. All the said things here are being done using ruby. Say I want to check the same on pure SQL, I wanted to see how.
Sample Object a Request table:
[{"ot_rqst_id":27460354,"rqst_type_cd":"NONTP","svc_type_cd":"TD","sl_ttl_type_cd":"","lot_num":41843022, ttl_stg_cd: 'CMPLT'}]
What I want to check on sql:
To see if the ttl_stg_cd of this object is equal to 'CMPLT'. If it is then update it to NULL else "something_else".
Let me remind you again that, I am already doing this in ruby language, but I am not an expert on sql so help would be appreciated.
The following query should do what you want.
I have taken the id from your query string. You need to put in the real table name and check the WHERE clause. If you remove the WHERE clause it will update all records in the table, you may wish the change it to something like
WHERE column_X = value_Y
UPDATE table_name
SET ttl_stg_cd = CASE ttl_stg_cd
WHEN 'CMPLT' THEN null
ELSE 'something_else' END
WHERE ot_rqst_id = 27460354;
I have a database full of stories, and each story belongs to a user.
I already have a user_id attribute for each story.
I recently added a user_name attribute to all my stories as well.
Now all of those user_name attributes are nil.
In the console, I can do Story.first.user_name = Story.first.user.name to set the user_name attribute to the first story's user.name value.
How can I iterate through all my stories in my Postgresql database and dynamically update each story's user_name value?
I want to use something like ActiveRecord's update_all method, but with a dynamic argument.
I tried in the Rails console:
Story.update_all('user_name = Story.user.name')
but that returned:
SQL (36.2ms) UPDATE "stories" SET user_name = Story.user.name
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "user"
LINE 1: UPDATE "stories" SET user_name = Story.user.name
^
: UPDATE "stories" SET user_name = Story.user.name
from (irb):14
Not sure how to use the FROM in raw SQL, or how to write the correct command in ActiveRecord.
The easiest way is to iterate over all records in the stories table:
Story.find_each {|story| story.update(user_name: story.user.name)}
If your goal is not to touch timestamps or validations as with update_all you can do
Story.find_each {|story| story.update_column(:user_name, story.user.name)}
I am having trouble returning a single value from a database using SQLite3 and Ruby.
A bit of context: I have a table of people and I would like to be able to query the database and get back the first name of someone in that table. I am using Rubymine with the SQLite3 gem.
When I run this code:
name = #db.execute <<-SQL
SELECT firstname FROM tbl_people WHERE email_address = '#{email_address}';
SQL
puts name
I get the following ouput:
{"firstname"=>"james", 0=>"james"}
When really the output I am expecting is:
james
If anyone can even point me in the right direction here it would be very much appreciated.
Cheers,
James
The execute() method creates a resultset, not just a single scalar value. You need to process the resultset to get the single value out of it.
name.each do |row|
puts row['firstname']
end
It sounds like you want to return the first value of the first row of a result set, and discard all other values and rows. The Database#get_first_value is what you want.
Also, for security you shouldn't inject strings into your SQL; allow the library to handle that for you.
query = "SELECT firstname FROM tbl_people WHERE email_address = ?;"
name = #db.get_first_value query, email_address
puts name
In a rails application I need to return an ActiveRecord collection containing multiple columns, but only 1 column must be unique. There is a default scope which is not the unique value.
Default Scope
default_scope { order('executed_at DESC') }
Current query WITHOUT unique :terms.
Search.where(organization_id: #my_array_of_ids)
.executed_after(from_date_time)
.limit(100)
.pluck(:terms)
executed_after(from_date_time) is a scope as well. How can I alter the above query to return unique terms without using the .uniq method? I want this to be a single SQL statement.
I also found this:
Convert SQL query to Active Record query in Rails 4
Notice the Model.distinct(:column)
EDIT:
I'd guess it is something like this, but I'd need more info:
Search.distinct(:terms).select(:executed_at)
.where(organization_id: #my_array_of_ids)
.executed_after(from_date_time)
.limit(100)
.group(:executed_at)
Hi I have this query in ms access which is somehow not working. All I need to do is to pull out donator name from donator table with the id of volunteer. But I need to get user input, volunteer name and pull out the related volunteer first. Please help.
SELECT volunteer.id, volunteer.name, donator.* FROM volunteer, donator WHERE Volunteer.id = Donator.vid AND Volunteer.name = Forms!frm5!Combo2;
Check your case on Database names. For instance, you reference volunteer.name and Volunteer.id. In some DB's, fields are case sensitive.
Also, you'd want to quote your volunteer name field.