Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMT self-referential relationship breaks while calling the inverse join relationship #2166

Closed
ghost opened this issue Jul 20, 2011 · 6 comments

Comments

@ghost
Copy link

ghost commented Jul 20, 2011

When I call the inverse relationship inside a lambda function the model breaks and I can't access successors anymore. I have also tried to rewrite the recursive function to use the family association directly, instead of breaking successors, inverse_families now breaks, i.e., it returns null instead of the associated records.

If I call predecessors.each inside a lambda function nothing breaks, although it breaks when calling successors.each as mentioned above.

Family

class Family < ActiveRecord::Base
  belongs_to   :successor, class_name: Shipyard.name,
                           inverse_of: :inverse_families,
                           counter_cache: true
  belongs_to :predecessor, class_name: Shipyard.name,
                           inverse_of: :families,
                           counter_cache: :inverse_families_count
end

Shipyard

class Shipyard < ActiveRecord::Base
  has_many         :families, foreign_key: :successor_id,
                              dependent: :restrict
  has_many     :predecessors, through: :families
  has_many :inverse_families, class_name: Family.name,
                              foreign_key: :predecessor_id,
                              dependent: :restrict
  has_many       :successors, through: :inverse_families

  attr_writer     :virtual_predecessors
  attr_accessible :virtual_predecessors

  invalid_predecessors = lambda { |record| record.successors_all }
  loop_error = "can't include records that would cause an indefinite loop"
  validates :virtual_predecessors, exclusion: { in: invalid_predecessors,
                                                message: loop_error }

  def predecessors_all(record = self)
    result = [ ]
    record.predecessors.each do |record|
      result << record.id << record.predecessors_all
    end
    result.flatten.uniq
  end

  def successors_all(record = self)
    result = [ ]
    record.successors.each do |record|
      result << record.id << record.successors_all
    end
    result.flatten.uniq
  end

  def virtual_predecessors
    if @virtual_predecessors.nil?
      predecessor_ids
    else
      @virtual_predecessors
    end
  end
end
@tenderlove
Copy link
Member

Can you post the schema.rb for this, a rails app, or possibly port it to the Rails test suite? I can't really fix this without a fully self contained test case.

@ghost
Copy link
Author

ghost commented Jul 21, 2011

Sure, I can post the schema.rb; however, I am not experienced enough to write a test suite...

create_table "families", :force => true do |t|
  t.integer  "successor_id"
  t.integer  "predecessor_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "families", ["predecessor_id"], :name => "index_families_on_predecessor_id"
add_index "families", ["successor_id"], :name => "index_families_on_successor_id"

create_table "shipyards", :force => true do |t|
  t.string   "name"
  t.integer  "families_count",         :default => 0
  t.integer  "inverse_families_count", :default => 0
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "shipyards", ["name"], :name => "index_shipyards_on_name", :unique => true

@jonleighton
Copy link
Member

@jlalin this may help you

@isaacsanders
Copy link
Contributor

@ghost Is this still an issue?

@ghost ghost assigned rafaelfranca Apr 30, 2012
@steveklabnik
Copy link
Member

Hi @ghost! Any updates? Are you still seeing this problem? We haven't heard from you in a while.

@frodsan
Copy link
Contributor

frodsan commented Oct 27, 2012

I'm closing this because we don't have enough information to reproduce this problem and there is no reply from @ghost. If this still persists, please update the ticket with clear reproduction steps or a sample app, and we'll reopen it.

Thanks!

@frodsan frodsan closed this as completed Oct 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants