我有一个表格“开关和快照”。一台Switch有很多快照,一个Snapshot属于一个Switch。我无法通过关联创建新的快照。在我的代码下面。
df1['zipcode'] = np.where(df1['address1'].isin(df2['address1']), df2['zipcode'], 'no_match')
ValueError Traceback (most recent call last)
<ipython-input-176-499624d43d5c> in <module>
----> 1 df1['zipcode'] = np.where(df1['address1'].isin(df2['address1']), df2['zipcode'], 'no_match')
2 df1
ValueError: operands could not be broadcast together with shapes (5,) (6,) ()
应用/模型
2.6.3 :014 > switch = Switch.new
=> #<Switch id: nil, switch: nil, ip_address: nil, created_at: nil, updated_at: nil>
2.6.3 :015 > switch.create_snapshot
NoMethodError (undefined method `create_snapshot' for #<Switch:0x00000000039950e0>)
通过迁移配置的psql
class Switch < ApplicationRecord
has_many :snapshots
end
class Snapshot < ApplicationRecord
belongs_to :switch
end
答案 0 :(得分:1)
首先创建开关,之后便可以创建类似这样的快照
switch = Switch.new
switch.save
or
switch = Switch.create(switch_params)
然后
switch.snapshots.create(snapshot_params)
如果要在创建交换机时创建快照,则应使用嵌套属性https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
答案 1 :(得分:0)
您需要在父模型上添加accepts_nested_attributes_for :model
。
嵌套属性允许您通过父级将属性保存在关联记录上。默认情况下,嵌套属性更新处于关闭状态,您可以使用#accepts_nested_attributes_for类方法启用它。启用嵌套属性时,将在模型上定义属性编写器。 (来自api.rubyonrails.org)
答案 2 :(得分:0)
要一起创建关联的对象,可以有这样的另一种方法
java.lang.NullPointerException
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748)
创建switch = Switch.new(swich_params)
switch.snapshots.build
switch.save
后,还将创建关联的switch
。