In the Rails do not prefer to use Time.now, Date.today etc We should always prefer to use it with zone like, Time.zone.now, Time.zone.today etc. but why so? Beacause when we fetch Time.now its always return system local time and ignore config.time_zone set in the application.rb. file. Here is the example tested in the rails console, 3.0.3 :001 > Time.now => 2022-04-19 09:57:58.413034249 +0530 3.0.3 :002 > Time.zone => #<ActiveSupport::TimeZone:0x000055704feec360 @name="UTC", @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=nil> 3.0.3 :003 > Time.zone.now => Tue, 19 Apr 2022 04:28:07.277000358 UTC +00:00 3.0.3 :004 > Time.zone = 'Sydney' => "Sydney" 3.0.3 :005 > Time.zone => #<ActiveSupport::TimeZone:0x000055705192d850 @name="Sydney", @tzinfo=#<TZInfo::DataTimezone: Australia/Sydney>, @utc_offset=nil> 3.0.3 :008 > Time.now => 2022...
Author has_many books books belongs_to author Named Scope scope :available, -> { where(available: true) } select 'books'.* from books where books.available = 't' scope :unavailable, -> { where(available: [nil, false]) } select 'books'.* from books where books.available = 'f' or books.available is null scope :public_status, -> { where(status: 'public') } 3.1.2 :004 > Article.public_status Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE "articles"."status" = ? AND "articles"."status" = ? [["status", "public"], ["status", "public"]] Here two times same status(‘public’) checked because one of the default scope and one of the named scope( public_status ). Author.joins(:books).merge(Book.available) select authors.* from authors inner join books on books.author_id=authors.id where books.available = 't' We can achi...
Never keep any secret data in public folder. Always use environment variables to keep secret data. Suppose we want to set postgresql username and password as environment variables in Ubuntu then do as, # type the following in the terminal to write in the .bashrc file. echo 'export MY_POSTGRESQL_USERNAME="rajkumar"' >> ~/.bashrc echo 'export MY_POSTGRESQL_PASSWORD="HJH34df@dff#@"' >> ~/.bashrc reload .bashrc as, source ~/.bashrc In order to print environment variables prepend it with $ as, echo $MY_POSTGRESQL_USERNAME echo $MY_POSTGRESQL_PASSWORD test environment variables as, echo $MY_POSTGRESQL_USERNAME rajkumar echo $MY_POSTGRESQL_PASSWORD HJH34df@dff#@ Also use ubuntu command printenv to print all the environment variables. then in the database.yml use these environment variables as, development: adapter: postgresql encoding: unicode host: localhost username: <%= ENV["MY_POSTGRESQL_USERNAME"] %> password: ...
Comments
Post a Comment
Let me know for any query..