2

So I have a database structure that looks something like this:

Events:

event_id: :integer
event_type: :string
name: :string
slug: :string
user_id: :integer
start: :datetime
finish: :datetime

and Weddings which I need to inherit from Events:

bride_name: :string
groom_name: :string
street: :string
city: :string
state: :string
zip: :integer
receptions: :text

I want to be able to create a Wedding, or any other event for that matter, and have it inherit the traits that are common among all the events from the Event model. When I tried to do this with STI the Wedding model only had the traits of the Event model. How do I implement this?

This does not work

class Wedding < Event

  attr_accessor :receptions, :reception_name, :reception_address, :reception_city, :reception_state, :reception_zip

end
watzon
  • 2,401
  • 2
  • 33
  • 65
  • Do you have a `type` column in your STI-table for Rails to save the type of the event in? Seems to be missing in your listing. – Marcus Ilgner May 09 '15 at 09:48
  • Yeah, when I tried STI I did. What I listed is the structure that I'd like to have. Even when I had a type column it just created an object with the attributes of the Event model – watzon May 09 '15 at 09:49
  • 1
    For one you shouldn't need any `attr_accessor` statements to access attributes from your model. It can make reading easier. Having a `type` column (with appropriate content) and inheriting in the Ruby code is enough for STI to work. If you want MTI, you can try https://github.com/hzamani/active_record-acts_as – Marcus Ilgner May 09 '15 at 10:02
  • @ma_il Please make that an answer so I can accept it! I had no idea such a gem existed! Also don't I need the attribute accessor for values that aren't in my database? All the things that I put as attribute accessor I'm planning on turning into an array and inserting into one part of the database – watzon May 09 '15 at 15:09
  • I just saw that you had both a field `receptions` in your db as well as in an `attr_accessor` call, the latter one being superfluous. – Marcus Ilgner May 11 '15 at 09:10

1 Answers1

1

As Rails only supports STI out of the box, if you really want MTI, you can give the ActiveRecord::ActsAs gem a shot.

There are a couple of open issues in their tracker but development seems to be somewhat active.

Marcus Ilgner
  • 6,935
  • 2
  • 30
  • 44