sti

ActiveRecord: query not using correct type condition for STI subclass

我的梦境 提交于 2021-01-27 00:33:07
问题 I have a set of STI subclasses inheriting from a User base class. I am finding that under certain conditions inside a subclass' definition, queries on the subclasses do not correctly use the type condition. class User < ActiveRecord::Base # ... end class Admin < User Rails.logger.info "#{name}: #{all.to_sql}" # ... end When loading the Rails console in development, it does what I would expect: Admin: SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('Admin') But when hitting the app

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

ぐ巨炮叔叔 提交于 2020-02-23 11:24:59
题目 One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A “Gang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads. Input Specification: Each input file

木棒

依然范特西╮ 提交于 2020-02-03 22:25:52
乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位。 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。 请你设计一个程序,帮助乔治计算木棒的可能最小长度。 每一节木棍的长度都用大于零的整数表示。 输入格式 输入包含多组数据,每组数据包括两行。 第一行是一个不超过64的整数,表示砍断之后共有多少节木棍。 第二行是截断以后,所得到的各节木棍的长度。 在最后一组数据之后,是一个零。 输出格式 为每组数据,分别输出原始木棒的可能最小长度,每组数据占一行。 数据范围 数据保证每一节木棍的长度均不大于50。 输入样例: 9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0 输出样例: 6 5 # include <bits/stdc++.h> using namespace std ; const int N = 70 ; int sti [ N ] , n ; bool use [ N ] ; int sum , len ; inline void init ( ) { memset ( sti , 0 , sizeof ( sti ) ) ; memset ( use , false , sizeof ( use ) ) ; sum = len = 0 ; } inline void read ( ) {

Single Table Inheritance in Rails 4

自闭症网瘾萝莉.ら 提交于 2020-01-15 06:42:18
问题 I'm trying to implement a somewhat simple STI in Rails 4, but there's something I can't yet manage to achieve. I have the following classes: class Person < ActiveRecord::Base end class NaturalPerson < Person end class LegalPerson < Person end class Employee < NaturalPerson end class Customer < NaturalPerson end The thing is, I have some attributes that I want to access only from the Employee class, some only from Customer, etc, but I can't find the way. If I were to be using Rails 3's way I

Rails 3 using MongoDB via mongoid adapter - is there any way to share attribute specifications without using Single-Table Inheritance?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-13 18:20:07
问题 Probably a confusing title, but not sure how else to put it. Example should make it clearer. I have many different models that share many of the same attributes. So in each model I have to specify those same attributes and THEN the attributes that are specific to that particular model. Is there any way I can create some class that lists these basic attributes and then inherit from that class without using Single-Table Inheritance? Because if I put all the shared attributes and Mongoid

performant ordering of keys in a MySQL compound index (WRT Rails Polymorphic associations and STI)

一世执手 提交于 2020-01-12 07:53:30
问题 Previously, I asked this question about compound indexes on polymorphic foreign keys in ActiveRecord. The basis of my question was my understanding that indexes should be based on the cardinality of your column, and there's generally pretty low cardinality on Rails's STI type and polymorphic _type columns. Accepting that the answer to my question is right -- that's there's value to indexing both the high cardinality _id columns and the low cardinality _type columns, because they together they

Achieve Single Table Inheritance with Sequelize

人走茶凉 提交于 2020-01-05 03:06:53
问题 Is there a way to use sequelize to create single table inheritance? I would like to have a STI for a Purchase and PartialPurchase model where I would have a type field which would be "Purchase" or "PartialPurchase" and classes Purchase and PartialPurchase which would each inherit from an Operation class. I don't see this as supported by sequelize, but is an implementation possible? 回答1: Hey so I know you didn't get an answer to this, in a reasonable time, but you can use a type column that's

Achieve Single Table Inheritance with Sequelize

我与影子孤独终老i 提交于 2020-01-05 03:06:25
问题 Is there a way to use sequelize to create single table inheritance? I would like to have a STI for a Purchase and PartialPurchase model where I would have a type field which would be "Purchase" or "PartialPurchase" and classes Purchase and PartialPurchase which would each inherit from an Operation class. I don't see this as supported by sequelize, but is an implementation possible? 回答1: Hey so I know you didn't get an answer to this, in a reasonable time, but you can use a type column that's

ActiveRecord builds instance of wrong class through a scope targeting an STI class

强颜欢笑 提交于 2020-01-04 06:44:17
问题 I would like to be able to call the build method on a scope that targets a certain class of model via its STI type, and have ActiveRecord build an instance of the correct class. class LineItem < ActiveRecord::Base scope :discount, where(type: 'DiscountLineItem') end class DiscountLineItem < LineItem; end > LineItem.discount.build # Expect an instance of DiscountLineItem here => #<LineItem ...> Here, I expected an instance of DiscountLineItem , not an instance of LineItem . 回答1: Even though

Different views for subclasses

拟墨画扇 提交于 2020-01-02 20:18:11
问题 I'm using STI subclasses and want to direct to different views for the different subclasses. At the moment I'm routing the subclass topic to the main class article like this: resources :topics, :controller => 'articles' Is there an easy way to direct to different views? Edit The best way I've found of doing this is: <% case%> <% when @article.type == 'Topic' %> <%= render 'topic' %> <% else %> <%= render 'article' %> <% end %> 回答1: If you name your views smartly enough, you could just do