简体   繁体   English

带有参数的Ruby on Rails模型关联

[英]Ruby on Rails Model Association with params

Here is my model 这是我的模特

class Location
  has_many :areas
end

class Area
  belongs_to :location
end

The Controller 控制器

def create
   @area = Area.new(area_params)
   @area.location_id = session[:current_location]
end

How can I associate this model when I'll create a new area? 创建新区域时如何关联此模型? something like this? 这样的东西?

def create
 @area = @location.areas.create(area_params)
end

Be sure to set the location_id attribute upon creation. 确保在创建时设置location_id属性。 It should be included in the params. 它应该包含在params中。 If you provide the full controller file I could be more specific. 如果提供完整的控制器文件,我可能会更具体。

Yes, include the area_params can include a location_id and it should be a column in the database. 是的,包括area_params可以包括location_id ,它应该是数据库中的一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM