sti

Declaring static properties of Rails model subclasses

≡放荡痞女 提交于 2019-12-11 14:01:51
问题 I'm new to Ruby and Rails (and programming!), and trying to figure out the idiomatic way of passing properties from a model to its STI children. I have a generic model 'Document', and some models that inherit from it — let's take 'Tutorial' as the example. I have a string field for an 'icon', in which I want to store the icon's filename but not full path (I think the path should be up to each model, as it's a detail of retrieving the record's data?): class Document < ActiveRecord::Base attr

Why does rails not respect the type of a belongs_to associated object with STI, when its superclass is abstract?

房东的猫 提交于 2019-12-10 16:03:17
问题 I've come across this rather odd bit of behaviour in a rails application I'm working on. I have multiple types of Post in an inheritance heirarchy, and a Post has_many FeedEntries. class Post < ActiveRecord::Base has_many :feed_entries end class Post::BlogPost < Post; end class Post::Discussion < Post; end class Post::Article < Post; end class FeedEntry < ActiveRecord::Base belongs_to :post end Now, when I have everything set up as before, calling FeedEntry#post on a saved object always

Rails app using STI — easiest way to pull these records?

时间秒杀一切 提交于 2019-12-08 01:48:22
问题 I'm learning my way around Rails and am working on a sample app to keep track of beer recipes. I have a model called Recipe which holds the recipe name and efficiency. I have a model called Ingredient which is using STI - this is subclassed into Malt, Hop, and Yeast. Finally, to link the recipes and ingredients, I am using a join table called rec_items which holds the recipe_id, ingredient_id, and info particular to that recipe/ingredient combo, such as amount and boil time. Everything seems

Rails STI and the setting of the “type” string

99封情书 提交于 2019-12-07 06:38:27
问题 I think I need to use STI in Rails. Here's my class: class Person < ActiveRecord::Base end class Landlord < Person end and the people table has a :type column that's a string. So, what I expect is to see in the table, is that every row that is a Person has the type set to "Person" and every Landlord has the type set to "Landlord". However, that's not what I see. Each Landlord has the type set to "Landlord", but all the Person have their type set to nil. This very well could be the way that

Rails app using STI — easiest way to pull these records?

a 夏天 提交于 2019-12-06 14:18:19
I'm learning my way around Rails and am working on a sample app to keep track of beer recipes. I have a model called Recipe which holds the recipe name and efficiency. I have a model called Ingredient which is using STI - this is subclassed into Malt, Hop, and Yeast. Finally, to link the recipes and ingredients, I am using a join table called rec_items which holds the recipe_id, ingredient_id, and info particular to that recipe/ingredient combo, such as amount and boil time. Everything seems to be working well - I can find all my malts by using Malt.all, and all ingredients by using Ingredient

Can I use ActiveRecord relationships with fields from an Hstore?

醉酒当歌 提交于 2019-12-06 09:08:14
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 record has a nightclub_id has_one :rp_boss #rp_boss should have a nightclub_boss_id has_one :captain

Rails 4 Devise Multiple User Models STI

安稳与你 提交于 2019-12-06 06:27:38
问题 Thanks for reading. I am using Devise and Rails 4. I want to add multiple User Models(admin, usertype1, usertype2) such that they inherit from the main User Model. I have searched many posts and come to the conclusion that I may use CanCan, which I do not want, or I may use Single Table Inheritance. The way I see it is to add a type string-column to my main User model I created with Devise. I will also need to extend each sub-class from the parent as in: class Admin < User end class Usertype1

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

浪子不回头ぞ 提交于 2019-12-05 19:17:55
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 includes into a single model and inherit from that base model in the other models, then STI is enforced and

Rails STI and the setting of the “type” string

ⅰ亾dé卋堺 提交于 2019-12-05 08:58:07
I think I need to use STI in Rails. Here's my class: class Person < ActiveRecord::Base end class Landlord < Person end and the people table has a :type column that's a string. So, what I expect is to see in the table, is that every row that is a Person has the type set to "Person" and every Landlord has the type set to "Landlord". However, that's not what I see. Each Landlord has the type set to "Landlord", but all the Person have their type set to nil. This very well could be the way that rails works, but I was just looking for some confirmation. This is, indeed, how STI works. The base class

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

自作多情 提交于 2019-12-05 00:07:41
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 this, is there a definitive source that explains this? The API docs did not seem to hit it on the head OR