Iterate over a collection/association model

26 views
Skip to first unread message

David Williams

unread,
Apr 12, 2016, 1:48:05 PM4/12/16
to rubyonra...@googlegroups.com
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.

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

shahroon ali

unread,
Apr 12, 2016, 1:49:51 PM4/12/16
to rubyonrails-talk
belongs_to :states should be :state singular.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/33864495c9bc99a4e62a62dfd435f649%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

David Williams

unread,
Apr 12, 2016, 1:59:06 PM4/12/16
to rubyonra...@googlegroups.com
shahroon ali wrote in post #1182801:
> belongs_to :states should be :state singular.
>
> On Tue, Apr 12, 2016 at 10:47 PM, David Williams <li...@ruby-forum.com>

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

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

Colin Law

unread,
Apr 12, 2016, 2:05:00 PM4/12/16
to Ruby on Rails: Talk


On 12 Apr 2016 6:58 p.m., "David Williams" <li...@ruby-forum.com> wrote:
>
> shahroon ali wrote in post #1182801:
> > belongs_to :states should be :state singular.
> >
> > On Tue, Apr 12, 2016 at 10:47 PM, David Williams <li...@ruby-forum.com>
>
> 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/.
>

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9bf1492db35264d858ecc0debcfb1de9%40ruby-forum.com.

David Williams

unread,
Apr 12, 2016, 2:11:25 PM4/12/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182803:
> On 12 Apr 2016 6:58 p.m., "David Williams" <li...@ruby-forum.com> wrote:
>>
>> shahroon ali wrote in post #1182801:
>> > belongs_to :states should be :state singular.
>> >
>> > On Tue, Apr 12, 2016 at 10:47 PM, David Williams <li...@ruby-forum.com>
>>
>> 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

David Williams

unread,
Apr 12, 2016, 3:39:30 PM4/12/16
to rubyonra...@googlegroups.com
I fixed it, thanks guys. The state_id was missing on the profile table.

Colin Law

unread,
Apr 12, 2016, 3:41:05 PM4/12/16
to Ruby on Rails: Talk
On 12 April 2016 at 19:10, David Williams <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1182803:
>> On 12 Apr 2016 6:58 p.m., "David Williams" <li...@ruby-forum.com> wrote:
>>>
>>> shahroon ali wrote in post #1182801:
>>> > belongs_to :states should be :state singular.
>>> >
>>> > On Tue, Apr 12, 2016 at 10:47 PM, David Williams <li...@ruby-forum.com>
>>>
>>> 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

>
> categories
> id
> name
> created_at
> updated_at
> description
> slug
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/427484d2eb63ca497a1af43aa77134af%40ruby-forum.com.

David Williams

unread,
Apr 12, 2016, 4:22:22 PM4/12/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182808:
> On 12 April 2016 at 19:10, David Williams <li...@ruby-forum.com> wrote:
>>>> undefined method `state_id' for #<Profile:0xd1b0b00>

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

I made a small mistake, I've been working on a lot lately. I forgot to
add it to the table.
Reply all
Reply to author
Forward
0 new messages