Not Null Contraint Rails - ruby-on-rails

I have a specific requirement, one column cannot be set as null once some value is added.
On create its value is set as null by default
Once the value is added to the column, it cannot be empty.
For such a case I am trying to add check_constraints
add_check_constraint :user, 'registery_id != null', name: 'reg_id
This constraint is added on both insert and update calls.
Rail Version 7
Ruby 3.x
I want it only to be executed in the case of an update only.

Related

Rails SQL select, doesn't seem to work as expected with the CASE statement

I'm trying to understand how to use the select method in queries to return custom columns.
I'm running Rails 5.2, database is postgresql.
m = Message.all.select("messages.*, CASE WHEN id > 30 THEN TRUE ELSE FALSE END AS above_30")
returns only the messages table with all its columns. How do I get the above_30 column, preferable eager loaded?
The above_30 is there, but Rails will not define an access for it because there is no column by that name in your schema.
You can define one yourself, or use m[:above_30] to access the "raw" attributes of the object as returned from the query.

How to test view and controller logic that relies on the created_at property?

I'm brand new to Ruby on Rails.
I have an application with a simple controller and view that render an HTML page with a list of Active Record objects from my database in least-recently-created (oldest first) order.
I'm attempt to write a test that verifies this sorting behavior by creating two objects with different created_at values, then verifying that the generated HTML has the oldest item first (using assert_select).
However, when I try to manually alter the created_at value on one of my objects (to ensure that my objects don't have the same created_at value), the other properties of the object appear to be set to a default value. For example, this code in my test results in an Image object with an url property value of http://www.example.com/image1.png, as expected:
#image1 = Image.create!(url: 'http://www.example.com/image1.png')
However, this code results in the url property value in the database unexpectedly changing to MyString:
#image1 = Image.create!(url: 'http://www.example.com/image1.png')
#image1.created_at = '2017-09-25'
#image1.save
I'm using ruby 2.3.3 and rails 5.1.1.1 (per my Gemfile).
I have two questions at this point:
Why does setting the created_at value on my object have the apparent side effect of changing the value of the url property to MyString? Is changing the created_at date just not allowed?
What's the proper way to go about arranging data in a test that exercises logic where the created_at dates of objects need to have particular values?
1) Are you using FactoryGirl?
2) Is an uploader class attached to the Image model?
3) Can you modify the Image object inside the test console?
$ RAILS_ENV=test bin/bundle exec bin/rails c
The type of the created_at attribute should be DateTime instead of String. You mess up the test object when trying to assign a string to the created_at attribute. Try the following
#image1.created_at = 2.days.from_now
or
#image1.created_at = 2.days.ago

postgres update the value of hstore with old partial value, in rails

I've been trying to update the wrong migrated values of the hash store,
"area_unit"=>NULL, "building_type"=>"{:building_type=>\"apartment\"}",
to
"area_unit"=>NULL, "building_type"=>"apartment"}",
the value should stay the same, I don't want to use regular expression. is there an easy way for doing that ? I'm working with ruby.
I was able to do it like the following, not a generic solution like I wanted, but it works.
SELECT id, properties::hstore ->'building_type' FROM stops WHERE (properties->'building_type' like '{:building_type=>\"apartment\"}');
UPDATE stops SET properties = properties || '"building_type"=>"apartment"'::hstore
WHERE (properties->'building_type' like '{:building_type=>\"apartment\"}');

Rails Ruby Logic for Dynamically updating specific Column Based on Param value

I have caller ID functionality working for my app but I am currently stumped on how to dynamically update the correct column based on the value of params[:call_number_type]
params[:call_number_type] can be "alt_phone", "cell_phone", "office_phone", or nil ... nil defaulting to "alt_phone" would be ideal.
Each of the above strings corresponds to a column name that I need to update if params[:call_number_type] has that value.
#contact = Contact.find(params[:contact_id])
if #contact.update(this_needs_to_be_the_right_column_key: params[:call_number])
The above update statement would have to be dynamically created based on the value of params[:call_number_type]
If some one could help me out that would be great. Thanks.
Try this
if #contact.update(call_number_type => params[:call_number])
...
private
def call_number_type
params[:call_number_type].present? ? params[:call_number_type] : :alt_phone
end

Custom constraints - postgres

This is more of a conceptual doubt. Is there any way in which we can apply a constraint that value of a field can't be blank? I know NOT NULL can be used. But I want to check if the field has only spaces, it rejects that value also. For example " " should be rejected.
In your Model's class, you can add a validation like this:
validates :field_name, presence: true
From the Rails Documentation:
This helper validates that the specified attributes are not empty. It
uses the blank? method to check if the value is either nil or a blank
string, that is, a string that is either empty or consists of
whitespace.
That will ensure that the value of that field can't be blank.
When validations are run?
Creating and saving a new record will send an SQL INSERT operation to
the database. Updating an existing record will send an SQL UPDATE
operation instead. Validations are typically run before these commands
are sent to the database. If any validations fail, the object will be
marked as invalid and Active Record will not perform the INSERT or
UPDATE operation. This avoids storing an invalid object in the
database.
See this for more details.
You could add the next psql constraints to prevent inserting null or " " strings in your DB without changing an application logics.
CREATE TABLE test (
string character varying(16) NOT NULL,
CONSTRAINT good_string CHECK (rtrim(string, ' ') != '')
);

Resources