Iterate over a collection/association model

I have a profile model that has a user state model (emotional state) - What I would like to do is give the user object the ability to change their state through the profile model. The code that I've written is below. I'm receiving "NoMethodError - undefined method 'state_id' for #<Profile:Obj %>" even though, they're associated with each other.

Profile.rb belongs_to :states

State.rb has_many :profiles

_state_form.html.erb

<%= simple_form_for @user, url: current_user, html: {multipart: true}, layout: :horizontal do |f| %>     <%= f.simple_fields_for :profile do |n| %>         <%= n.label :state_id, 'Select a state' %>         <%= n.collection_select :state_id, State.all, :id, :name %>         <%= f.submit 'Change State', class: 'btn btn-info-outline' %>     <% end %> <% end %>

I seeded the names of the states into the db table. Because, I don't want anyone altering the records to add their own.

<%= render 'users/shared/state_form' %>

Maybe, I'm missing something, but this approach seems to work without issue for categories.

belongs_to :states should be :state singular.

shahroon ali wrote in post #1182801:

belongs_to :states should be :state singular.

I fixed the typo, but it's still not working.

undefined method `state_id' for #<Profile:0xd1b0b00>

shahroon ali wrote in post #1182801:

belongs_to :states should be :state singular.

I fixed the typo, but it’s still not working.

undefined method `state_id’ for #Profile:0xd1b0b00

Is there a column of that name in the profiles table?

Colin

Colin Law wrote in post #1182803:

shahroon ali wrote in post #1182801: > belongs_to :states should be :state singular. >

I fixed the typo, but it's still not working.

undefined method `state_id' for #<Profile:0xd1b0b00>

Is there a column of that name in the profiles table?

Colin

-- Posted via http://www.ruby-forum.com/.

--

I thought the table automatically creates the _id extension by default.

My categories table is nearly identical to the state table.

categories id name created_at updated_at description slug

I fixed it, thanks guys. The state_id was missing on the profile table.

Colin Law wrote in post #1182803:

shahroon ali wrote in post #1182801: > belongs_to :states should be :state singular. >

I fixed the typo, but it's still not working.

undefined method `state_id' for #<Profile:0xd1b0b00>

Is there a column of that name in the profiles table?

Colin

-- Posted via http://www.ruby-forum.com/.

--

I thought the table automatically creates the _id extension by default.

How can a table automatically create a column? If you have profile belongs_to state then you have to create a migration to put the state_id column in the database. I think you need to follow the advice that has now nearly worn out my keyboard, so many times have I typed it: You should work right through a good tutorial such as railstutorial.org (which is free to use online). That will show you the basics of Rails.

My categories table is nearly identical to the state table.

What has the categories table got to do with it? I thought this problem is about profiles and states.

Colin

Colin Law wrote in post #1182808: