Rails 多态关节 table 空
Rails Polymorphic joint table empty
我正在创建一个事件系统,但我的多态关联有困难。
事件可以是:
- 全国:全群广播(一个群有多家店铺)
- 本地 : 为一个或多个商店广播
我有2种歌曲,达到阈值后会同时播放:
- specific_media : 达到门槛的店铺播放媒体
- generic_media : 当特定媒体在达到门槛的店铺播放时,该媒体在该组的其他店铺播放。
我的模特:
class Event < ActiveRecord::Base
belongs_to :specific_media, polymorphic: true
belongs_to :generic__media, polymorphic: true
has_many :event_join
accepts_nested_attributes_for :event_joins, allow_destroy: true
class EventJoin < ActiveRecord::Base
belongs_to :eventable, polymorphic: true
belongs_to :event
end
class Shop < ActiveRecord::Base
has_many :event_joins, as: :eventable
has_many :events,
:through => :event_joins
end
class Group < ActiveRecord::Base
has_many :event_joins, as: :eventable
has_many :events,
:through => :event_joins
end
表:
class CreateEvents < ActiveRecord::Migration
def up
create_table :events do |t|
t.string :title
t.references :specific_media, polymorphic: true
t.references :generic_media, polymorphic: true
t.string :created_by_id
t.string :updated_by_id
t.float :threshold
t.timestamps
end
end
def down
drop_table :events
end
end
class CreateEventJoins < ActiveRecord::Migration
def up
create_table :event_joins do |t|
t.integer :event_id
t.references :eventable, polymorphic: true
t.boolean :is_active, default: true
t.string :created_by
t.string :updated_by
end
add_index :event_joins, [:eventable_id, :eventable_type]
end
def down
drop_table :event_joins
end
end
我的控制器:
def index
@search = Search.new(params[:search])
@shop = find_user_shop(@search.shop_id)
@events = @shop.events
end
def new
@event = Event.new
end
def create
@search = Search.new(params[:search])
@shop = find_user_shop(@search.shop_id)
@events = @shop.events
@event = @events.new(params[:event])
if @event.save!
flash.now[:notice] = "Votre événement a bien été enregistré"
render :index
else
flash.now[:error] = "Erreur lors de l'enregistrement du événement"
render :new
end
end
我希望 eventable_type 是 Shop(本地)或 Group(全国)。所以主要是我想要的是当我输入:
- Shop.first.events:我得到了所有本地事件(eventable_type:商店)
- Group.first.events:全国赛事我都搞定了(eventable_type:组)
我该怎么做?我在这个多态关联中出错了吗?
此致,
放弃连接 table:
groups.rb
# setup:
# $ gem install active_record
# $ gem install logger
# $ gem install pry
# $ gem install sqlite3
#
# run:
# $ ruby groups.rb
require 'active_record'
require 'logger'
require 'pry'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Schema.define do
self.verbose = false
create_table :events do |t|
t.string :title
t.references :eventable, polymorphic: true, index: true
t.string :created_by_id
t.string :updated_by_id
t.float :threshold
end
create_table :shops do |t|
t.integer :group_id
end
create_table :groups do |t|
end
end
class Event < ActiveRecord::Base
belongs_to :eventable, polymorphic: true
end
class Shop < ActiveRecord::Base
belongs_to :group
has_many :events, as: :eventable
end
class Group < ActiveRecord::Base
has_many :shops
has_many :events, as: :eventable
end
group = Group.create
shop1 = group.shops.create
shop2 = group.shops.create
group.events.create(title: "Global Event: SpaceX Rocket Launch")
shop1.events.create(title: "Local Event: Son's Birthday")
shop2.events.create(title: "Local Event: Dinner Party")
# Associations you can try to verify it works:
# shop1.events
# shop2.events
# group.events
# shop1.group.events
binding.pry
我正在创建一个事件系统,但我的多态关联有困难。
事件可以是:
- 全国:全群广播(一个群有多家店铺)
- 本地 : 为一个或多个商店广播
我有2种歌曲,达到阈值后会同时播放:
- specific_media : 达到门槛的店铺播放媒体
- generic_media : 当特定媒体在达到门槛的店铺播放时,该媒体在该组的其他店铺播放。
我的模特:
class Event < ActiveRecord::Base
belongs_to :specific_media, polymorphic: true
belongs_to :generic__media, polymorphic: true
has_many :event_join
accepts_nested_attributes_for :event_joins, allow_destroy: true
class EventJoin < ActiveRecord::Base
belongs_to :eventable, polymorphic: true
belongs_to :event
end
class Shop < ActiveRecord::Base
has_many :event_joins, as: :eventable
has_many :events,
:through => :event_joins
end
class Group < ActiveRecord::Base
has_many :event_joins, as: :eventable
has_many :events,
:through => :event_joins
end
表:
class CreateEvents < ActiveRecord::Migration
def up
create_table :events do |t|
t.string :title
t.references :specific_media, polymorphic: true
t.references :generic_media, polymorphic: true
t.string :created_by_id
t.string :updated_by_id
t.float :threshold
t.timestamps
end
end
def down
drop_table :events
end
end
class CreateEventJoins < ActiveRecord::Migration
def up
create_table :event_joins do |t|
t.integer :event_id
t.references :eventable, polymorphic: true
t.boolean :is_active, default: true
t.string :created_by
t.string :updated_by
end
add_index :event_joins, [:eventable_id, :eventable_type]
end
def down
drop_table :event_joins
end
end
我的控制器:
def index
@search = Search.new(params[:search])
@shop = find_user_shop(@search.shop_id)
@events = @shop.events
end
def new
@event = Event.new
end
def create
@search = Search.new(params[:search])
@shop = find_user_shop(@search.shop_id)
@events = @shop.events
@event = @events.new(params[:event])
if @event.save!
flash.now[:notice] = "Votre événement a bien été enregistré"
render :index
else
flash.now[:error] = "Erreur lors de l'enregistrement du événement"
render :new
end
end
我希望 eventable_type 是 Shop(本地)或 Group(全国)。所以主要是我想要的是当我输入:
- Shop.first.events:我得到了所有本地事件(eventable_type:商店)
- Group.first.events:全国赛事我都搞定了(eventable_type:组)
我该怎么做?我在这个多态关联中出错了吗?
此致,
放弃连接 table:
groups.rb
# setup:
# $ gem install active_record
# $ gem install logger
# $ gem install pry
# $ gem install sqlite3
#
# run:
# $ ruby groups.rb
require 'active_record'
require 'logger'
require 'pry'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Schema.define do
self.verbose = false
create_table :events do |t|
t.string :title
t.references :eventable, polymorphic: true, index: true
t.string :created_by_id
t.string :updated_by_id
t.float :threshold
end
create_table :shops do |t|
t.integer :group_id
end
create_table :groups do |t|
end
end
class Event < ActiveRecord::Base
belongs_to :eventable, polymorphic: true
end
class Shop < ActiveRecord::Base
belongs_to :group
has_many :events, as: :eventable
end
class Group < ActiveRecord::Base
has_many :shops
has_many :events, as: :eventable
end
group = Group.create
shop1 = group.shops.create
shop2 = group.shops.create
group.events.create(title: "Global Event: SpaceX Rocket Launch")
shop1.events.create(title: "Local Event: Son's Birthday")
shop2.events.create(title: "Local Event: Dinner Party")
# Associations you can try to verify it works:
# shop1.events
# shop2.events
# group.events
# shop1.group.events
binding.pry