具有查询表的Rails多态 [英] Rails Polymorphic with Lookup Table

查看:189
本文介绍了具有查询表的Rails多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行此操作的最佳方法是什么?我希望能够通过多态性赋予乐队和艺术家流派.我可以使用habtm和has_many:through来做到这一点,但是我试图找出是否可以通过多态来实现.

What's the best way to do this? I want to be able to give Bands and Artists genres through polymorphism. I can do it with habtm and has_many :through but I'm trying to figure out if it's possible through polymorphism.

GenreList将是一个查找表,其中列出了不同流派(例如朋克,流行,金属).我已经审查了Ryan Bate关于多态关联"的截屏视频,但是我仍然很困惑.具体来说,我不确定如何创建多态表Genre,该表将根据GenreList模型(查找表)提供罐装类型.

GenreList would be a lookup table with a list of different genres (e.g. Punk, Pop, Metal). I've reviewed Ryan Bate's screencast for Polymorphic Assoiciations but I'm still stuck. Specifically, I'm not sure how to create the polymorphic table Genre which would be fed canned genres from the GenreList model (the lookup table).

以下正确吗?

rails generate model Genre genre_list_id:integer genreable_id:integer genreable_type:string

class Artist < ActiveRecord::Base
  has_many :genres, :as => :genreable
end

class Band < ActiveRecord::Base
  has_many :genres, :as => :genreable
end

class Genre < ActiveRecord::Base
  belongs_to :genreable, :polymorphic => true
end

class GenreList < ActiveRecord::Base
end

推荐答案

好的,经过6.5小时后,我设法弄清楚了这一点.我使用了Inherited_resources gem来帮助控制器.回顾一下,我希望能够通过多态关系将流派添加到艺术家和乐队,即流派是一个查找表,流派将是一个多态模型,其中包含艺术家和乐队的流派.以下是对我有用的代码:

Ok, after 6.5 hrs, I managed to figure this out. I used the inherited_resources gem to help with the controllers. To recap, I wanted to be able to add Genres to Artists and Bands through a polymorphic relationship, i.e. Genres would be a lookup table, and Genreings would be a polymorphic model that contains genres for Artists and Bands. Below is the code that worked for me:

# Generate some scaffolding
rails generate scaffold Artist name:string
rails generate scaffold Band name:string
rails generate scaffold Genre name:string
rails generate scaffold Genreing genre_id:integer genreable_id:integer genreable_type:string

# Models  
class Artist < ActiveRecord::Base
    has_many :genreings, :as => :genreable
    has_many :genres, :through => :genreings
end

class Band < ActiveRecord::Base
    has_many :genreings, :as => :genreable
    has_many :genres, :through => :genreings
end

class Genre < ActiveRecord::Base
    attr_accessible :name
    has_many :genreings
end

class Genreing < ActiveRecord::Base
    attr_accessible :genre, :genre_id, :genreable, :genreable_type, :genreable_id
    belongs_to :genre
    belongs_to :genreable, :polymorphic => true
end

# Controller
class GenreingsController < InheritedResources::Base
    belongs_to :genreable, :polymorphic => true
end

# Artist Form View
= simple_form_for(@artist) do |f|

  .inputs
    = f.input :name
    = f.association :genres, :as => :check_boxes

  .actions
    = f.button :submit

# Band Form View
... (Similar to Artist)

这篇关于具有查询表的Rails多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆