3

I am using raw sql queries in my rails application. I want to insert NULL value in one my rows, like how active record does using nil so that i will get that row when i do Table.find_by_column(nil). I tried '#{nil}' its entering as nothing db column is blank like this ' ' i want it to be NULL.

3
  • what query you are using, show the complete query how you are trying to do it Commented Jun 1, 2015 at 6:21
  • "INSERT into table_name (r_id, r_name) VALUES (0,'#{nil}')" Commented Jun 1, 2015 at 6:28
  • try this "INSERT into table_name (r_id, r_name) VALUES (0,null)" Commented Jun 1, 2015 at 6:28

5 Answers 5

2

You can also try this.

sql = "INSERT into table_name (r_id, r_name) VALUES (1, null)"

records_array = ActiveRecord::Base.connection.execute(sql)
Sign up to request clarification or add additional context in comments.

Comments

1

How about nil instead of #{nil}

Table.column = nil

Comments

0

try this

Model.find_by_sql "SELECT * FROM table  where column is NULL"

1 Comment

Thank u @SachinR but i want it like like that while inserting it should be raw sql but while searching it will be ActivRecord.
0
Model.find_by_column(nil)

This will give record which column's value is nil

Comments

0

You can update all records using:

Model.update_all(:field_name => nil)

Then you can find all records that are nil using:

Model.where(:field_name => nil)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.