ricardoerib
ricardoerib

Reputation: 53

Mongoid 5.0 - Rails 4 - Collection Form has_many

I can't get my form working width collection when using has_many in mongoid

Model Line:

class Line
  include Mongoid::Document
  include Mongoid::Timestamps

  field :observations
  field :position, :type => Integer
  field :status, :type => Integer

  has_many :unities, :inverse_of => :unity
end

Model Unity:

class Unity
  include Mongoid::Document
  include Mongoid::Timestamps      
  field :prefix, type: Integer
  field :owner_name
  field :owner_email
  field :owner_phone
  field :document      
  field :license
  field :color
  field :active, type: Mongoid::Boolean, default: false
  field :qrx, type: Mongoid::Boolean, default: false
  belongs_to :line, index: true
end

My form is:

<%= bootstrap_form_for @line do |f| %>
    <div class="col-md-2">      
        <%= f.collection_select :unity_id, Unity.all, :id, :title %>
    </div>
<% end %>

I'm getting this error: undefined method `unity_id' for #

Upvotes: 2

Views: 339

Answers (1)

pangpang
pangpang

Reputation: 8821

there are something wrong with your form:

<%= bootstrap_form_for @line do |f| %>
    <div class="col-md-2">      
        <%= f.collection_select :unity_id, Unity.all, :id, :title %>
    </div>
<% end %>

@line has many unities, it doesn't have unity_id field. You are also not defined the title field in Line model.

maybe you can do like this:

<%= bootstrap_form_for @unity do |f| %>
    <div class="col-md-2">      
        <%= f.collection_select :line_id, Line.all, :id, :owner_name %>
    </div>
<% end %>

Upvotes: 1

Related Questions