rails-activerecord

How to define the sequence to use when creating a table in ActiveRecord migration in Ruby on Rails 5.2?

别来无恙 提交于 2020-06-26 06:26:48
问题 I need to assign a specific Postgres sequence to the ID field of my table. In the model, I tried to define the following setup which has no effect on Posgres: class MyObject < ActiveRecord::Base self.sequence_name = "global_seq" Usually, a table definition in ActiveRecord migrations start with create_table "objects", id: :serial, force: :cascade do |t| which generates a Postgres definition of column default value as default nextval('objects_id_seq'::regclass) How can I specify in the

Property from attr_accessor does not show when I render json

半腔热情 提交于 2020-06-01 04:49:13
问题 In Rails, I have a User model. I added attr_accessor :score to the model. If I render the JSON for one user, I do not see the "score" attribute. Why is this? user = User.find(3) user.score = 55 render json: user 回答1: render json: user, methods: [:score] attr_accessor is alternative for getter and setter method so it is a method and as I have mentioned we can call it as above 来源: https://stackoverflow.com/questions/30641623/property-from-attr-accessor-does-not-show-when-i-render-json

Property from attr_accessor does not show when I render json

你离开我真会死。 提交于 2020-06-01 04:49:10
问题 In Rails, I have a User model. I added attr_accessor :score to the model. If I render the JSON for one user, I do not see the "score" attribute. Why is this? user = User.find(3) user.score = 55 render json: user 回答1: render json: user, methods: [:score] attr_accessor is alternative for getter and setter method so it is a method and as I have mentioned we can call it as above 来源: https://stackoverflow.com/questions/30641623/property-from-attr-accessor-does-not-show-when-i-render-json

Property from attr_accessor does not show when I render json

坚强是说给别人听的谎言 提交于 2020-06-01 04:49:06
问题 In Rails, I have a User model. I added attr_accessor :score to the model. If I render the JSON for one user, I do not see the "score" attribute. Why is this? user = User.find(3) user.score = 55 render json: user 回答1: render json: user, methods: [:score] attr_accessor is alternative for getter and setter method so it is a method and as I have mentioned we can call it as above 来源: https://stackoverflow.com/questions/30641623/property-from-attr-accessor-does-not-show-when-i-render-json

How to use Rails with uppercase column name?

六月ゝ 毕业季﹏ 提交于 2020-05-29 05:00:50
问题 I have the following as part of an AR query: .having('COUNT(foo.id) > bar.maxUsers') This generates an error: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column bar.maxusers does not exist ^ HINT: Perhaps you meant to reference the column "bar.maxUsers". I am referencing the column bar.maxUsers . So, apparently AR downcases the query. How to suppress this behavior? Rails 4.2.10 PostgreSQL EDIT: SQL: SELECT ... HAVING COUNT(foo.id) > bar.maxUsers So it is happening after the to

Rails - Active Record: add extra select column to find(:all)

ぐ巨炮叔叔 提交于 2020-05-12 11:36:06
问题 I want to add new column sum(time_entries.hours) to sql select I'm querying entries like this: issues = Issue.visible.where(options[:conditions]).all( :include => ([:status, :project, :time_entries] + (options[:include] || [])).uniq, :conditions => statement, :order => order_option, :joins => query_joins(order_option.join(',')), :limit => options[:limit], :offset => options[:offset], :group => "#{Issue.table_name}.id" ) It generates this select: SELECT "issues"."id" AS t0_r0, ... "time

Rails change column type and update column values

两盒软妹~` 提交于 2020-03-18 12:18:47
问题 I have a table called Users with a column Active that is type boolean. I will soon need to change the type of that column to string. I will also need to change all of the column values of Active from :true to "active" and :false to "inactive". To change the column type I would use Change a column type from Date to DateTime during ROR migration class ChangeColumnTypeInUsers < ActiveRecord::Migration def up change_column :users, :active, :string end def down change_column :users, :active,

Add a reference column migration in Rails 5

跟風遠走 提交于 2020-02-20 06:21:09
问题 A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like? Related question for Rails 3: Rails 3 migrations: Adding reference column? Related question for Rails 4: Add a reference column migration in Rails 4 回答1: As with prior versions of Rails, you may use the following command to create the migration: rails g migration AddUserToUploads user:references Unlike prior versions of Rails, the migration looks like: class

How to get the value from Postgres computed function upon object creation in Rails

瘦欲@ 提交于 2020-02-07 03:46:11
问题 A few months ago, I asked a question on how to increment the value of a field in alphabetical order in before create. After a few comments, we arrived at a very nice solution I currently have the requirement to extend this solution now and return the object and display it on the same page upon creation. I need Ajax to do this. The current problem is I have no idea how to attach the created setup_code to the JSON object that is returned upon creation. I have tried the following: before

Route concern and polymorphic model: how to share controller and views?

僤鯓⒐⒋嵵緔 提交于 2020-01-30 18:48:47
问题 Given the routes: Example::Application.routes.draw do concern :commentable do resources :comments end resources :articles, concerns: :commentable resources :forums do resources :forum_topics, concerns: :commentable end end And the model: class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true end When I edit or add a comment, I need to go back to the "commentable" object. I have the following issues, though: 1) The redirect_to in the comments_controller.rb would be