Got ActiveRecord::AssociationTypeMismatch`Component(#70354292616060) expected, got String(#70354278277000)` when create object "Machine" below;

**Objective**: Try to assign "attributes" to a model that is referred to the other model.

**Question**: I would like to hear from expert that what is the best practice for this kind of operation. 

**Settings**:

Rails 3.2.12 / Ruby 1.9.3-p194:

Models:

```
class Machine < ActiveRecord::Base
  attr_accessible :cpu, :name, :ram

  belongs_to :cpu, class_name: "Component", foreign_key: "component_id"
  belongs_to :ram, class_name: "Component", foreign_key: "component_id"
end

class Component < ActiveRecord::Base
  attr_accessible :name
  has_many :machines
end

```

Views for Machine:

```
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :cpu %><br />
    <%= f.collection_select :cpu, Component.all, :id, :name %>
  </div>
  <div class="field">
    <%= f.label :ram %><br />
    <%= f.collection_select :ram, Component.all, :id, :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

```

**Procedure**:

1. Create few objects to Component such that `id:1 name:"cpu1"` & `id:2 name:"ram1"`
2. Go to machines\_path and create Machine with selecting cpu1 and ram1 from pull down
3. Got following error when submit `Component(#70354292616060) expected, got String(#70354278277000)`

I tried to change :cpu and :ram in View with :cpu\_id and :ram\_id. Thereafter, I can create the model without error. However I cannot access cpu & ram directly from machine;

```
1.9.3-p194 :001 > Machine.first
  Machine Load (0.1ms)  SELECT "machines".* FROM "machines" LIMIT 1
 => #<Machine id: 2, name: "test", cpu_id: 1, ram_id: 1, created_at: "2013-05-06     16:42:47", updated_at: "2013-05-06 16:42:47"> 
1.9.3-p194 :002 > Machine.first.cpu
  Machine Load (0.2ms)  SELECT "machines".* FROM "machines" LIMIT 1
 => nil 
1.9.3-p194 :003 > Machine.first.ram
  Machine Load (0.2ms)  SELECT "machines".* FROM "machines" LIMIT 1
 => nil 

```

So I had to create following model methods

```
  class Machine < ActiveRecord::Base

  (snip)

  def cpu
    Component.find_by_id(self.cpu_id)
  end

  def ram
    Component.find_by_id(self.ram_id)
  end

```

Then I can get expected output

```
1.9.3-p194 :002 > Machine.first.cpu.name
  Machine Load (0.2ms)  SELECT "machines".* FROM "machines" LIMIT 1
  Component Load (0.2ms)  SELECT "components".* FROM "components" WHERE "components"."id" = 1 LIMIT 1
  => "CPU1" 

```

But I feel it's redundant and looking for a simpler way to do this.

Appreciate if any suggestions.