Select from predefined set of records

Greetings people of elixirforum, I have a noob question about phoenix and ecto associations and forms for them.
My problem is - I have a user profile schema with belongs_to :location, MyApp.Location and respectively has_many :profiles, MyApp.Profile in Location schema. and somewhere in template for creating profile

       <%= select location, :id, @locations %>
<% end %>``` where @locations is a list of ```{city, id}```tuples. In the end of all these I want a user just to select a city he/she is from. In that current implementation when user selects a city and saves profiles, my code creates brand new Location record instead of referencing existing location. 
I don't want to manually insert id of a location to my profile record, how could I do it the right way?

update: for now I went with ```<%= select f, :location_id, @locations %>```, still curious if that is the right way to do it

Basically yep, look it up then link in the controller calls.

1 Like