On Mar 25, 2010, at 4:43 PM, Alex Bowyer wrote:

Hi Matt,
Thanks

Yes, hard coded, this is a one time, time-specific app that won't be used again.

so if i just have three fields

first_choice
second_choice
third_choice

what do I have to do to make sure rails and hobo knows they are foreign keys? I only know how to specify foreign keys via has_many etc


A straightforward way to set this up:

Voter has_one :vote

class Vote < ActiveRecord::Base
  hobo_model

  fields do
    timestamps
  end

  belongs_to :voter
  belongs_to :first_choice, :class_name => 'Choice'
  belongs_to :second_choice, :class_name => 'Choice'
  belongs_to :third_choice, :class_name => 'Choice'
end

class Choice
  hobo_model

  fields do
    name :string
  end

has_many :first_votes, :class_name => 'Vote', :foreign_key => 'first_choice_id' has_many :second_votes, :class_name => 'Vote', :foreign_key => 'second_choice_id' has_many :third_votes, :class_name => 'Vote', :foreign_key => 'third_choice_id'

end

---------

I split the Vote model off only because it's cleaner, and accommodates the inevitable, "can we do another vote" requests. You can also drop an auto_actions_for :user, [:new, :create] onto the VotesController and get pretty URLs like:

/users/1234/votes/new

With this setup, you *should* get a decent automatic create page with select-one dropdowns for first/second/third choices.

Hope this helps!

--Matt Jones

--
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en.

Reply via email to