我有一个rails表,例如:
class CreateContent < ActiveRecord::Migration
def change
create_table :contents do |t|
t.references :data_book, null: false, index: true
t.string :room_name, null: false
t.references :client, null: false, index: true
t.timestamps
end
add_foreign_key :contents, :data_books, on_delete: :cascade
add_foreign_key :contents, :clients, on_delete: :cascade
end
end
我的模型指定2
belongs_to
来自的关联
contents
到
data_books
和
clients
我不确定为这个添加新数据实例的正确方法是什么;官员
Rails documentation
指定使用
build_{association_name}
但我做不到,因为我有两种不同的联想。
这是正确的方法吗:
Content.new(
data_book: DataBook.find(content_creation_params[:data_id]),
room_name: @room_name,
client: Client.find(content_creation_params[:client_id]),
)
还是有更好、更具红宝石色彩的方法?