0

I have the following models:

class User
  include Mongoid::Document
  has_one :gender

class Gender
  include Mongoid::Document
  has_and_belongs_to_many :users

Gender table is seeded with genders.

My simpleform for user edit is as follows:

<%= simple_form_for @user do |f| %>
  <%= f.input :gender, :collection => Gender.all, :prompt => "Choose a gender" %>
  <%= f.submit "Save" %>
<% end %>

However when I save this, a user.gender doesn't do anything.

Anyone have any thoughts on this?

4

2 回答 2

1

From what I can see your logic is like this -

-one user has one gender

-one gender has many users

class User
  include Mongoid::Document
  belongs_to :gender

class Gender
  include Mongoid::Document
  has_many :users

To get user's gender

user.gender

To users who has the same gender

gender.users
于 2013-04-11T07:02:17.670 回答
0

Change your model as

class User
  include Mongoid::Document
  has_one :gender

class Gender
  include Mongoid::Document
  belongs_to :user

And change your form field as:

<%= f.input :gender_id, :collection => Gender.all, :prompt => "Choose a gender" %>
于 2013-04-11T06:41:04.617 回答