gregbell/active_admin — Issue #872: Problem with has_one and :foreign_key
Author: boynamedbri
Created: 2011-12-22 23:32:31+00:00
State: open

I apologize ahead of time if I'm making a silly mistake here. I'm not yet very experienced with Rails.

I have a migration like this:

```ruby
def up
create_table :questions do |t|
t.text :text
end

create_table :answers do |t|
t.integer :question_id
t.integer :next_question_id

t.string :text
end
end
```

I then have Question and Answer models like this:

```ruby
class Question < ActiveRecord::Base
has_many :answers, :dependent => :destroy
end

class Answer < ActiveRecord::Base
belongs_to :question
has_one :next_question, :class_name => "Question", :foreign_key => 'next_question_id'
end
```

These relationships seem to work okay in the console:

```ruby
irb(main):007:0> q1 = Question.new
=> #
irb(main):008:0> q1.text = "Question 1 (Parent)"
=> "Question 1 (Parent)"
irb(main):009:0> q2 = Question.new
=> #
irb(main):010:0> q2.text = "Question 2 (Next)"
=> "Question 2 (Next)"
irb(main):011:0> a = Answer.new
=> #
irb(main):012:0> a.question = q1
=> #
irb(main):013:0> a.next_question = q2
=> #
irb(main):014:0> a.question
=> #
irb(main):015:0> a.next_question
=> #
```

However, when I try to create an Answer in Active Admin I encounter 2 issues. Firstly, the "new" page lists the belongs_to Question but not the has_one.

Next, if I click "Create Answer" I see the following SQL error:

```sql
SQLite3::SQLException: no such column: questions.next_question_id: SELECT "questions".* FROM "questions" WHERE "questions"."next_question_id" = 1 LIMIT 1
```