activerecord

In Rails models; for symbols get automatically converted to YAML when saving to DB. What is the correct approach?

孤街醉人 提交于 2019-12-10 11:29:11
问题 In my model example Game, has a status column. But I usually set status by using symbols. Example self.status = :active MATCH_STATUS = { :betting_on => "Betting is on", :home_team_won => "Home team has won", :visiting_team_won => "Visiting team has one", :game_tie => "Game is tied" }.freeze def viewable_status MATCH_STATUS[self.status] end I use the above Map to switch between viewable status and viceversa. However when the data gets saved to db, ActiveRecord appends "--- " to each status. So

Rake db:setup or rake db:create applying wrong username/wrong database

我与影子孤独终老i 提交于 2019-12-10 11:17:54
问题 Using Mac OSX Yosemite (10.10.4): rails -v => Rails 4.2.3 ruby -v => ruby 2.2.2p95 Followed a combination of these instructions: https://www.digitalocean.com/community/tutorials/how-to-setup-ruby-on-rails-with-postgres and these instructions: http://blog.endpoint.com/2014/03/setup-rails-environment-with-postgresql.html bash: $createuser -P -d -e project (This was successful) $password: aPassword $re-enter password: aPassword $rails new project --database=postgresql $cd project/config/database

ActiveRecord Scoped Uniqueness Validations vs MySQL Compound Unique Indexes

余生长醉 提交于 2019-12-10 11:16:55
问题 First, let me set the stage: I have an app_settings table that joins app_setting_options with user_id , user_role_id , event_id , group_id , or app_level_setting . For any given app_setting there should be a specific app_setting_option_id and only one of those other objects. For each one of those secondary objects, I have specified a uniqueness validation with a scope in the AppSetting model: validates :user_id, uniqueness: { scope: :app_setting_option_id}, allow_nil: true validates :user

Rails database-specific data type

时光毁灭记忆、已成空白 提交于 2019-12-10 11:15:17
问题 This is the first time I've tried out Rails with PostgreSQL. I am confused by the lack of timestamp with timezone usage in code I usually see regarding columns storing absolute time. A prime example is devise, it stores columns a datetime column which translates to a timestamp with out timezone in PostgreSQL. I consider it a bad practice because timestamp with out timezone could mean any time zone. If you were accessing the database from another application, you would have no idea which time

using like in join with code igniter active records

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:13:57
问题 I am having issues with a particular like in my active records query. When I use join('users parent', 'child.treePath LIKE CONCAT(parent.treePath,"%")') Code igniter spits out JOIN 'users' parent ON 'child'.'treePath' 'LIKE' CONCAT(parent.treePath,"%") (note that I have replaced all back ticks ( ` ) with ( ' ) due to markdown :/) So, the issue is that code igniter is wrapping LIKE in ( ` ). How can I tell it to not attempt to format this block? Complete query: $this->db->select('child.uuid')

Codeigniter update_batch() with included update of the where key

[亡魂溺海] 提交于 2019-12-10 11:09:24
问题 I want to update multiple rows in the database with codeigniters update_batch() function. But the field specified in the where should also be changed. The following code should make it clear: $set = array( array( 'token' => '65787131678754', 'device' => 'none', 'new_token_value' => '' ), array( 'token' => '75798451315464', 'device' => 'none', 'new_token_value' => '' ) ); $this->db->update_batch(TBL_NAME, $set, 'token'); Tokens specified in token should be updated with device to 'none' and the

Yii2: How to put null values to the end of an object list when sorting is ascending?

有些话、适合烂在心里 提交于 2019-12-10 11:07:55
问题 Here is my DataProvider: $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => ['pageSize' => 50], 'sort' => [ 'defaultOrder' => [ 'priority' => SORT_DESC, 'date_targeted' => SORT_ASC ] ] ]); What I want is to move ("not set") to the end of the results. How can I do this? 回答1: You may add new field in select . And set in IF case like that: $query->select([ '*', new \yii\db\Expression('IF(date_targeted IS NULL, 1, 0) AS date_targeted_flag') ]); And in sort add date

What important differences are there between Rails' development and production environments?

若如初见. 提交于 2019-12-10 10:59:19
问题 I experienced a horrible problem today as a result of differences between Rail's production and development environments. Consider the code: "select * from subscription_plans where affiliate_id is null or affiliate_id = #{@subscription_plan.affiliate.id rescue 0};" There will never be any affiliates with an id of 0, so if @subscription_plan.affiliate is nill I expected the query to return only subscription plans without an affiliate. Works great in development environment because nil.id

codeigniter active record, generate, but do not execute query

余生颓废 提交于 2019-12-10 10:47:08
问题 I am working a library that needs to be fed sql queries as strings todo its job. I am using CodeIgniter and the active record implementation of their database class. I know that I can echo the SQL statement like so... But I only want to generate this query, not execute it. echo $this->db->last_query(); Any help accomplishing this would be great! 回答1: The last_query() function you are using returns the query string, it doesn't execute the it. Try assigning the function return to a variable for

Ruby on Rails 3: How can I sort ActiveRecords by an attribute of another table?

ⅰ亾dé卋堺 提交于 2019-12-10 10:46:32
问题 I need to query a database table and get the rows ordered by a count of an association. Is there a Rails (like Active Record Query) way to do this? My models and their associations are as follows: class User < ActiveRecord::Base has_one :business end class Business < ActiveRecord::Base has_many :postulations end class Postulation < ActiveRecord::Base belongs_to :business end I need to get a number of Users ordered by the amount of Postulations that their Business has. Is there a clean way to