简体   繁体   中英

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. It should be included in the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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