activerecord

Rails: Improve performance of simple searching method

拈花ヽ惹草 提交于 2019-12-24 20:28:22
问题 I have been building an app these days. The functionality is nothing fancy at all, I have to connect to a client's SOAP webservice, fetch some data, save it into my pg database and build a search functionality based on this data. The search has to be performed on two tables, both combined are like 80K rows. It needs to look for every word in the input text in several fields from these two tables, which have a classical assocation one to many. Previous to get my hands dirty I was looking at

ActiveRecord thinks I'm using an integer, but I am not

末鹿安然 提交于 2019-12-24 20:00:09
问题 Rails 5.2.3 MySQL gem (mysql server is version 8) I do not want to use the automatically generated id, for migrations. In app/models/concerns/shared.rb: module Shared extend ActiveSupport::Concern def generate_unique_string_id_for_model self.id = "#{Time.now.to_f.to_s.gsub(".","_")}_#{self.class.name}" if self.id.blank? # Sample output: 1566326250_176106_Role end end In my app/models/roles.rb: class Role < ApplicationRecord include Shared audited belongs_to :user before_create :generate

ERROR: update or delete on table “users” violates foreign key constraint “fk_rails_03de2dc08c” on table “comments”

最后都变了- 提交于 2019-12-24 19:41:21
问题 I am stuck on this error and not able to figure out whats wrong. rails aborted! ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_03de2dc08c" on table "comments" DETAIL: Key (id)=(11) is still referenced from table "comments". : DELETE FROM "users" WHERE "users"."id" = $1 #31 回答1: Sounds like user number 11 made some comments and the user hence cannot be deleted because those comments still refer to

Multipe relationships in Rails where the class name does not match the association name

不想你离开。 提交于 2019-12-24 19:39:57
问题 I have a private message model that relates to two users, how do I setup an association so that PM.sender is the sender's user model and PM.receiver is recipient's user model? (so that I can call PM.sender.username etc.) I have a sender_id and receiver_id field. 回答1: Assuming model classes Message and User , in your Message model: class Message < ActiveRecord::Base belongs_to :sender, :class_name => 'User' belongs_to :receiver, :class_name => 'User' end Because the class name can't be deduced

Ruby on Rails - Sum calculation not working with associations in model

最后都变了- 提交于 2019-12-24 19:39:01
问题 I have a User table with a column total_user_xp with a has_many :goals association. The Goal table has a column total_goal_xp which is calculated in the model with the following method: # goal.rb belongs_to :user has_many :goal_activities has_many :activities, through: :goal_activities def total_goal_xp self.total_goal_xp = self.goal_activities.sum(:total_xp) end For some reason, in my controller and model file for User, I can't calculate the sum for total_goal_xp where the user_id matches

Activeresource, updating and merging

半城伤御伤魂 提交于 2019-12-24 19:07:04
问题 I've run into a problem I'm not able to handle right now with an ActiveResource object that looks something like this: #<Settings:0x000000085fff48 @attributes= {"account_id"=>1, "created_at"=>"2012-01-10T14:54:36Z", "id"=>1, "settings_hash"=> #<Settings::SettingsHash:0x000000085ff250 @attributes= {"email_notices"=> #<Settings::SettingsHash::EmailNotices:0x0000000860c860 @attributes= {"none"=> ["none", "none"]}, @persisted=false, @prefix_options={}>, "permissions"=> #<Settings::SettingsHash:

Rails: Increament in a datetime column in all tables

牧云@^-^@ 提交于 2019-12-24 18:52:16
问题 Lets say I have 20 tables and every table has column updated_at I want to update all the rows in all the tables such as every row's updated_at field should increase 1 hour. How can I do it with avoiding to update active records object I would prefer to run a sql query instead on every table one query for one table. 回答1: If you ask this question in the database admin section of SO I think you'll get a much better response. I tried googling and can find: https://www.w3schools.com/sql/func

has_many through association with existing object / ActiveRecord

人走茶凉 提交于 2019-12-24 18:15:53
问题 Given models class Composition < ActiveRecord::Base attr_accessible :content has_many :compositions_tags has_many :tags, :through => :compositions_tags end class Tag < ActiveRecord::Base attr_accessible :text has_many :compositions_tags has_many :compositions, :through => :compositions_tags validates_uniqueness_of :tag, only: [:create, :update], message: "already taken" end class CompositionsTag < ActiveRecord::Base belongs_to :composition belongs_to :tag end Now, for example I do Composition

ActiveRecord find and only return selected columns aligned with [:id]

妖精的绣舞 提交于 2019-12-24 17:08:28
问题 I have a set of data that is in two different tables. I want to join them up on my /show page in my ruby on rails app. In the controller, I have it set to find the date, set a variable, use that date to look through the other database to pull the needed information. My controller looks like this: def show @ticket = Ticket.find(params[:id]) @hellodate = Ticket.select(:date) @winnings = Winnings.find(:all, :conditions => {:date => @hellodate}) respond_to do |format| format.html # show.html.erb

ActiveRecord relation using an array of foreign keys

扶醉桌前 提交于 2019-12-24 17:02:39
问题 Is that possible to establish a relationship between Tree and Branch such as: class Tree < ActiveRecord::Base has_many :branches end class Branch < ActiveRecord::Base belongs_to :tree end But with an array of foreign keys branch_ids stored in Tree? I know it's the opposite of the default process, but I want to do so (just for testing). Many thanks for any help. 回答1: As Lichtamberg mentioned it is a bad schema. Since you said "just for testing", if branch ids will be a column with comma