Search code examples
ruby-on-railsdatabaseruby-on-rails-3relationships

How do I describe a relationship like "issue has status" in Rails 3?


I am developing an "issue tracker" app in Rails 3 but I am having trouble describing the relationship between an "Issue" and its "Status". Each issue can only have one status - I'd like this to be a combo box in HTML - but clearly each status can be assigned to multiple issues. I started with "issue has one status" but then I ran into problems retrieving that status in the view. I assume this is correct, but I must need something else...


Solution

  • You should be using Issue belongs_to :status and Status has_many :issues instead.

    When you use a has_one association, Rails will look for the foreign key issue_id in the statuses table. This is not what you want. You want issues to point to statuses, not vice versa.