sti

Rails Question: belongs_to with STI — how do i do this correctly?

我怕爱的太早我们不能终老 提交于 2020-01-02 01:02:24
问题 I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused. I have a few questions based on a model configuration similar to: class Parental < ActiveRecord::Base end class Mother < Parental has_many :babies end class Father < Parental has_many :babies end class Baby < ActiveRecord::Base belongs_to :?????? end What should Baby belong_to? In terms of a migration, what should i name/add for foreign key on the babies table? I've had a hard time researching

STI and form_for problem

橙三吉。 提交于 2019-12-29 21:14:29
问题 I am using Single Table Inheritance for managing different types of projects. Models: class Project < ActiveRecord::Base end class SiteDesign < Project end class TechDesign < Project end Edit action from projects_controller: def edit @project = Project.find(params[:id]) end View edit.html.erb: <% form_for(@project, :url => {:controller => "projects",:action => "update"}) do |f| %> ... <%= submit_tag 'Update' %> <% end %> Update action of projects_controller: def update @project = Project.find

Alternative to Rails Single Table Inheritance (STI)?

时间秒杀一切 提交于 2019-12-24 01:16:00
问题 I have a model and table that I believe is perfectly suited to STI. My table is called Finances and has two types: Income and Expenses. Besides type there are three other columns: description , amount , and date . I'm getting very nervous using STI in Rails, since it requires some hacking. I'm too new to Rails to hack up the code. Even though it works, I don't understand it. That seems dangerous. My question is, how do I set up my model, controller, and view if I do NOT use STI? Any best

Rails Sti: single path, different controller

风格不统一 提交于 2019-12-23 20:12:21
问题 Have STI classes: class Page < ActiveRecord::Base belongs_to :user end class FirstTypePage < Page end class SecondTypePage < Page end Controllers for each class, class PageController < AplicationCorroller end class FirstTypePageController < PageController end class SecondTypePageController < PageController end And routings: resources :user resource :page end How to handle FirstTypePage by FirstTypePageController, SecondTypePage by SecondTypePageController on single path? i.e. user/1/page/2 is

Can I use ActiveRecord relationships with fields from an Hstore?

对着背影说爱祢 提交于 2019-12-22 18:22:01
问题 Can I tie a model to another through active record belongs_to using a field from an hstore hash? I'll elaborate: I have a User model that gets subclassed via STI on one of its fields to many different other User models based on permissions: class User < ActiveRecord::Base self.inheritance_column = :role #other definitions and validations end Here's one such submodel, the nightclub_boss model, meant for administrative users for my app: class NightclubBoss < User belongs_to :nightclub #the boss

Testing simple STI with FactoryGirl

无人久伴 提交于 2019-12-20 11:06:28
问题 I have got a class, that is the base of some other classes that specializes the behavior: class Task < ActiveRecord::Base attr_accessible :type, :name, :command validates_presence_of :type, :name, :command # some methods I would like to test end The class CounterTask inherits from Task class CounterTask < Task end This all works fine until I am trying to test the base class, since it must have a type. FactoryGirl.define do factory :task do sequence(:name) { |n| "name_#{n}" } sequence(:command

ElasticSearch with Tire doesn't include custom analyzer with STI model

人走茶凉 提交于 2019-12-13 03:24:15
问题 I have an STI model which I want to be searchable with ElasticSearch and Tire. The issue I am having is when Tire creates the mappings it seems to ignore my custom analyzers for the second model. Below is an example of my models. class Account < ActiveRecord::Base attr_accessible :name, :type include Tire::Model::Search include Tire::Model::Callbacks tire.settings :analysis => { :analyzer => { "custom_search_analyzer" => { "tokenizer" => "keyword", "filter" => "lowercase" }, "custom_index

Rails STI validation inheritance

拟墨画扇 提交于 2019-12-12 18:17:39
问题 I have STI models in my Rails application. The ancestor model has validations with the validates_... methods which are working fine. But I have custom validations as well, and I would like to add more different custom validations in the descendants. These custom validations would depend on the class. If I write class DescendantA < Ancestor protected def validate # ... end end It simply overwrites the original validations, so I loose the original inherited validations. Is there a convention to

Waterline default query condition

允我心安 提交于 2019-12-12 09:13:36
问题 How can I set the default where/sort condition in my SailsJS waterline model? In Rails I would use default scope. 回答1: Sails doesn't support default criteria on a per-model basis, but if you're using blueprint routes you can set default criteria for the route by overriding in your config/routes.js file, for example: "GET /user": { controller: 'user', action: 'find', where: {'deleted': false}, sort: 'age DESC' } This will work even if you don't have a find action defined in your UserController

Should I namespace STI models?

天涯浪子 提交于 2019-12-11 19:21:59
问题 I have the following models: module Core class Conditioner include Mongoid::Document field :operator, type: String, default: '' # can be !=, ==, <, >, <=, >=, = end end module Core class Count < Conditioner field :threshold, type: Integer, default: 0 # a simple threshold end end module Core class Time < Conditioner UNITS = %w(seconds minutes hours days weeks months years) # will be use like this: Time.value.send(Time.unit) Ex: 3.minutes field :value, type: Integer, default: 0 # 3 field :unit,