ordering a many_to_many relationship in rails
I would like to implement a drag and drop of a list of items in a playlist. Playlist contents are of different activerecord types. I need to store the position and persist it in the database and handle the ordering using acts_as_list. I'm not sure I'm building the associations correctly.
I built has_many_through relationships using a join table, but I'm not sure how to get acts_as_list to work with this configuration.
My models are like this:
class Playlist < ActiveRecord::Base
belongs_to :group
belongs_to :user
has_many :links, through: :playlists_contents dependent: :destroy
has_many :medias, through: :playlists_contents dependent: :destroy
end
My migration for the join table looks like this:
class CreateJoinTable < ActiveRecord::Migration[5.0]
def change
create_table :contents_playlists do |t|
t.belongs_to :link, index: true
t.belongs_to :media, index: true
t.belongs_to :playlist, index: true
end
end
I have this so far in my join table model:
class PlaylistsContents < ActiveRecord::Base
default_scope -> { order(position: :asc) }
default_scope :order => 'position'
belongs_to :playlist
belongs_to :link
belongs_to :media
acts_as_list :scope => :link
acts_as_list :scope => :media
end
ruby-on-rails ruby activerecord ruby-on-rails-5 acts-as-list
add a comment |
I would like to implement a drag and drop of a list of items in a playlist. Playlist contents are of different activerecord types. I need to store the position and persist it in the database and handle the ordering using acts_as_list. I'm not sure I'm building the associations correctly.
I built has_many_through relationships using a join table, but I'm not sure how to get acts_as_list to work with this configuration.
My models are like this:
class Playlist < ActiveRecord::Base
belongs_to :group
belongs_to :user
has_many :links, through: :playlists_contents dependent: :destroy
has_many :medias, through: :playlists_contents dependent: :destroy
end
My migration for the join table looks like this:
class CreateJoinTable < ActiveRecord::Migration[5.0]
def change
create_table :contents_playlists do |t|
t.belongs_to :link, index: true
t.belongs_to :media, index: true
t.belongs_to :playlist, index: true
end
end
I have this so far in my join table model:
class PlaylistsContents < ActiveRecord::Base
default_scope -> { order(position: :asc) }
default_scope :order => 'position'
belongs_to :playlist
belongs_to :link
belongs_to :media
acts_as_list :scope => :link
acts_as_list :scope => :media
end
ruby-on-rails ruby activerecord ruby-on-rails-5 acts-as-list
add a comment |
I would like to implement a drag and drop of a list of items in a playlist. Playlist contents are of different activerecord types. I need to store the position and persist it in the database and handle the ordering using acts_as_list. I'm not sure I'm building the associations correctly.
I built has_many_through relationships using a join table, but I'm not sure how to get acts_as_list to work with this configuration.
My models are like this:
class Playlist < ActiveRecord::Base
belongs_to :group
belongs_to :user
has_many :links, through: :playlists_contents dependent: :destroy
has_many :medias, through: :playlists_contents dependent: :destroy
end
My migration for the join table looks like this:
class CreateJoinTable < ActiveRecord::Migration[5.0]
def change
create_table :contents_playlists do |t|
t.belongs_to :link, index: true
t.belongs_to :media, index: true
t.belongs_to :playlist, index: true
end
end
I have this so far in my join table model:
class PlaylistsContents < ActiveRecord::Base
default_scope -> { order(position: :asc) }
default_scope :order => 'position'
belongs_to :playlist
belongs_to :link
belongs_to :media
acts_as_list :scope => :link
acts_as_list :scope => :media
end
ruby-on-rails ruby activerecord ruby-on-rails-5 acts-as-list
I would like to implement a drag and drop of a list of items in a playlist. Playlist contents are of different activerecord types. I need to store the position and persist it in the database and handle the ordering using acts_as_list. I'm not sure I'm building the associations correctly.
I built has_many_through relationships using a join table, but I'm not sure how to get acts_as_list to work with this configuration.
My models are like this:
class Playlist < ActiveRecord::Base
belongs_to :group
belongs_to :user
has_many :links, through: :playlists_contents dependent: :destroy
has_many :medias, through: :playlists_contents dependent: :destroy
end
My migration for the join table looks like this:
class CreateJoinTable < ActiveRecord::Migration[5.0]
def change
create_table :contents_playlists do |t|
t.belongs_to :link, index: true
t.belongs_to :media, index: true
t.belongs_to :playlist, index: true
end
end
I have this so far in my join table model:
class PlaylistsContents < ActiveRecord::Base
default_scope -> { order(position: :asc) }
default_scope :order => 'position'
belongs_to :playlist
belongs_to :link
belongs_to :media
acts_as_list :scope => :link
acts_as_list :scope => :media
end
ruby-on-rails ruby activerecord ruby-on-rails-5 acts-as-list
ruby-on-rails ruby activerecord ruby-on-rails-5 acts-as-list
asked Nov 21 '18 at 5:52
AyradAyrad
1,67573469
1,67573469
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You'll need a position field in the contents_playlists (or playlists_contents - as the model is called PlaylistsContents?) table. You can add multiple scopes by using:
acts_as_list scope: [:playlist, :link, :media]
Edit: added :playlist
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
1
No, and if you don't mind the types mixing, you can just scope by:playlist
– lafeber
Nov 21 '18 at 9:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405992%2fordering-a-many-to-many-relationship-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You'll need a position field in the contents_playlists (or playlists_contents - as the model is called PlaylistsContents?) table. You can add multiple scopes by using:
acts_as_list scope: [:playlist, :link, :media]
Edit: added :playlist
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
1
No, and if you don't mind the types mixing, you can just scope by:playlist
– lafeber
Nov 21 '18 at 9:20
add a comment |
You'll need a position field in the contents_playlists (or playlists_contents - as the model is called PlaylistsContents?) table. You can add multiple scopes by using:
acts_as_list scope: [:playlist, :link, :media]
Edit: added :playlist
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
1
No, and if you don't mind the types mixing, you can just scope by:playlist
– lafeber
Nov 21 '18 at 9:20
add a comment |
You'll need a position field in the contents_playlists (or playlists_contents - as the model is called PlaylistsContents?) table. You can add multiple scopes by using:
acts_as_list scope: [:playlist, :link, :media]
Edit: added :playlist
You'll need a position field in the contents_playlists (or playlists_contents - as the model is called PlaylistsContents?) table. You can add multiple scopes by using:
acts_as_list scope: [:playlist, :link, :media]
Edit: added :playlist
edited Nov 21 '18 at 9:19
answered Nov 21 '18 at 8:59
lafeberlafeber
1,0511421
1,0511421
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
1
No, and if you don't mind the types mixing, you can just scope by:playlist
– lafeber
Nov 21 '18 at 9:20
add a comment |
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
1
No, and if you don't mind the types mixing, you can just scope by:playlist
– lafeber
Nov 21 '18 at 9:20
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
so I don't have to create a third polymorphic model called content that the other models inherit from that should be positionable?
– Ayrad
Nov 21 '18 at 9:11
1
1
No, and if you don't mind the types mixing, you can just scope by
:playlist– lafeber
Nov 21 '18 at 9:20
No, and if you don't mind the types mixing, you can just scope by
:playlist– lafeber
Nov 21 '18 at 9:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405992%2fordering-a-many-to-many-relationship-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
