Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HABTM > has_many, through #76

Closed
davidwessman opened this issue Mar 20, 2015 · 7 comments
Closed

HABTM > has_many, through #76

davidwessman opened this issue Mar 20, 2015 · 7 comments

Comments

@davidwessman
Copy link
Member

@jforberg have been speaking about it, for example in #69 .

We should merge from our HABTM table to an actual model, which gives us the ability to actually validate before the join is created.

How do you move the data from the HABTM-join table to a new one?
@fsek/admins

@jforberg
Copy link
Contributor

The database doesn't change at all, only the models.

Read: http://stackoverflow.com/questions/24559489/rails-4-habtm-custom-validation-on-associations#answer-24559591

Maybe you want to rename the table to something more descriptive but that's it.

@davidwessman
Copy link
Member Author

Makes sense! Do you add id's as well?
(Does the UserRole-table need an own ID column?)

@jforberg
Copy link
Contributor

Yes. All tables have id columns in rails.

@davidwessman
Copy link
Member Author

@jforberg

unless table_exists? :elections_posts
      create_table "elections_posts", id: false do |t|
        t.integer "election_id"
        t.integer "post_id"
      end
    end

This is standard for all the HABTM-tables that we have. So should we add IDs?

@jforberg
Copy link
Contributor

@davidwessman Yes. Also we should ensure that we remove any duplicates (this is the other problem with HABTM, there is zero protection from duplicates).

The reason for this is, we need a primary key on the table. Otherwise, we can't tell different rows apart or delete a row without also maybe deleting other rows. It's madness. (ok well no but it sucks a bit).

In classic SQL you would make a compound primary key of both election_id and post_id (and this would work) but rails wants us to always have a separate ID column so that's that.

Knowledge about database design is one of those things which can really help understanding a lot. In rails, we're told to just sort of forget about the database which is a bit sad.

@davidwessman
Copy link
Member Author

davidwessman commented Sep 23, 2015

  • app/models//album.rb:5: has_and_belongs_to_many :album_categories
  • app/models//album.rb:6: has_and_belongs_to_many :subcategories
  • app/models//album_category.rb:3: has_and_belongs_to_many :albums
  • app/models//election.rb:5: has_and_belongs_to_many :posts
  • app/models//post.rb:7: has_and_belongs_to_many :elections
  • app/models//subcategory.rb:3: has_and_belongs_to_many :albums

Ready for refactoring 😄

@davidwessman
Copy link
Member Author

Finally not in use anymore! 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants