views:

288

answers:

1

OK, this one is a doozy. I have an ActiveRecord object that, among other things, includes a relationships as follows:

class Sample < ActiveRecord::Base
  has_and_belongs_to_many :related_samples,
                          :class_name => "Sample",
                          :join_table => "related_samples",
                          :foreign_key => "sample_id"
                          :associated_foreign_key => "related_id"
end

And here's the scheme for it:

def self.up
  create_table :samples do |t|
    t.string :related_info
    t.string :name
    #There's other info, but it is not related to this problem
  end

  create_table :related_samples, :id => false do |t|
    t.references :sample
    t.references :related
    t.timestamps
  end
end

This works perfectly. When I ask for sample_object.related_samples, it gives me whatever other Sample objects I assigned it.

The problem comes with the edit action for my views. My goal is to allow a user to replace an existing related Sample object to a different one by selecting it from a list of all available Samples. And I want to implement this (if possible) inside of a fields_for helper method, so that doing the update is really simple. I'm not sure how to implement this, or I even can. Is it possible? And if so, how?

A: 

I just realized something extremely important that likely makes this question impossible to solve. When I say I want to be able to change a Sample object another one is related to, I mean the reference to it. Example:

Sample1.related_sites == [Sample2, Sample3]

I want to do something so that I can use Sample4 in place of another Sample, so that it becomes:

Sample1.related_sites == [Sample2, Sample4]

But I want Sample3 to remain intact. This means the references need to change, which I see no way of doing in any way that would be easier than modifying my database scheme. So, if you still have an idea, I'd love to hear it, but I'll likely be changing things. Thanks, SO community!

G. Martin