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?