Questions tagged [sti]

Single Table Inheritance - a mechanism to add the principle of object-oriented inheritance onto relational database models by having child classes map onto the same table as their ancestors.

In Single Table Inheritance, types are discriminated by a field in the table that indicates what class in the hierarchy the object belongs to.

For more information see:

Single Table Inheritance - Martin Fowler - Patterns of Enterprise Application Architecture

384 questions
0
votes
2 answers

has_and_belongs_to_many STI, restrict to be unique by types

I'm using STI models with has_and_belongs_to_many relations. So I have User with many Templates of different types, like MainTemplate < Template; NotSoMainTemplate < Template; etc. Is there a way to limit each user to have only one MainTemplate…
alex29
  • 73
  • 7
0
votes
1 answer

How to reassign STI class to a variable within model's method?

I've got STI like this: class Post end class Post::Confirmed < Post end class Post::Draft < Post def confirm! becomes Post::Confirmed end end ...# somewhere in controller # POST /posts/1/confirm # POST /posts/1/confirm.json def confirm …
IlyaDoroshin
  • 4,659
  • 4
  • 22
  • 26
0
votes
1 answer

How do I whitelist these Rails4 params?

I am using this in my controller: def step_params params.require(@type.underscore.to_sym).permit( :id, :name, :note, :position, :institution_id, :protocol_id, :sequence_id,:orientation_id, step_item_attributes: [:id, :note, :name,…
Perry Horwich
  • 2,798
  • 3
  • 23
  • 51
0
votes
1 answer

rails single-table inheritance preserve virtual attributes after reinitializing with becomes

In my controller (where current_request is a virtual attribute of Service: Working as expected: @service = Service.new(name: service_params[:name], current_request: request) puts @service.current_request.blank? # outputs false Using '.becomes'…
doug
  • 1,963
  • 2
  • 16
  • 24
0
votes
1 answer

Rails 4 Paperclip not saving image to db with STI

I can not get paperclip to save images to the database, I have used different paperclip versions (3.5.0, 4.2 and from the git repo). At present I am using 4.2 as recommended in the Gem's Docs. class Instructor < ActiveRecord::Base #also tried…
Jax
  • 1,839
  • 3
  • 18
  • 30
0
votes
1 answer

Rails 4 multiple similar models... STI?

I have 3-4 different types of clients for a photo site. Musician, wedding, portrait, and general with the possibility of adding more types. The idea is each type will have a shared set of attributes such as name email etc. but each will have their…
Phix
  • 9,364
  • 4
  • 35
  • 62
0
votes
1 answer

Relating my models/tables? STI? Polymorphic?

I currently have three models: Business, Charity, and Organization. A business and charity are both types of organizations because they share many similar attributes (e.g. address, hours, website, etc.) however they each have their own unique…
0
votes
1 answer

Unexpected NoMethodError after migration to add new column

I have a player model inheriting from a user model to share authentication logic (devise) with a 3rd model (coach that also inherits from user). class User < ActiveRecord::Base end class Player < User end class Coach < User end I'm trying to add a…
computer_smile
  • 2,117
  • 2
  • 24
  • 42
0
votes
1 answer

Rails single-table inheritance

I have a class TrafficRule that is defined like this: TrafficRule - name:string - type:string - details:text The details parameter will be a JSON object that will store a set of details for a given traffic rule, and the type parameter will define…
kid_drew
  • 3,857
  • 6
  • 28
  • 38
0
votes
1 answer

STI where children include parent records (Rails 4)

BACKGROUND: I have City model and I'd like users to be able to select both a Hometown and a Current City from the master list of Cities, so as to be able to call user.hometown and user.currentcities as methods. QUESTION: What are some approaches for…
neon
  • 2,811
  • 6
  • 30
  • 44
0
votes
1 answer

How to use the parent class view in STI rails

I have a Class Meeting from which derives MeetingOnline and MeetingOnSite. I want to be able to use the views of Meeting for both MeetingOnline and MeetingOnSite. Now when i do <%= render @meetings %> it asks me for…
user3442206
  • 577
  • 1
  • 6
  • 22
0
votes
2 answers

Active Record Single Table Inheritance Join Table

I have a users table and a course table. On top of the user model, I'm using STI to have a teacher and a student model. A teacher has_many courses, but a student has_and_belongs_to_many courses. A course belongs_to a teacher and…
0
votes
1 answer

Rails / Using Single Table Inheritance with non-english type values

I'm considering using single table inheritance for my contacts model in order to implement different behaviour for different kinds of contacts. As I understand it Rails determines which model.rb, controllers, routes and so on to use by looking at…
0
votes
0 answers

Multiple Devise Models or Single User Model

I have a rails 4.1 application setup with Devise. I have: one USER model and one PROFILE model. The user has_one profile However, I have two types of users - Buyers and Sellers (it's a service platform and I have to use states to check if the user…
gaurav
  • 31
  • 1
  • 4
0
votes
1 answer

Rails Multiple relationships and STI

Iam setting up user accounts and users have different roles. Iam using STI and my models look like: class User < ActiveRecord::Base end Class Teacher < User end Class Student < User end How can i set up a many to many relationship between…
beesasoh
  • 2,075
  • 2
  • 15
  • 17