Rails如何创建一个具有多态关联的新记录?

时间:2019-02-04 03:28:55

标签: ruby-on-rails activerecord ruby-on-rails-5

我有一个yield_single_example=False模型,该模型通过Location是多态的

一个:locatable有一个Event

Location

class Event < ApplicationRecord has_one :location, as: :locatable end 中,如何通过参数传递位置来创建新事件?

events_controller#create

但是,Event.location是def create property = Event.create! params.require(:event).permit(:name, :time, :location, :organizer_id, attachments: []) # ... other stuff, and finally render JSON response end 。如何为活动创建位置?

谢谢

1 个答案:

答案 0 :(得分:1)

number = int(input('Enter an integer: ')) has_divisors = False for divisor in range (2, number): if number % divisor == 0: print (divisor) has_divisors = True if not has_divisors: print (number, 'has no divisors in the range') 表包含一个名为locations的外键,该外键与locatable_id表建立了多态关联。

要创建属于events的{​​{1}}:

events_controller.rb

location