[Rails] Re: Modelling Foreign Keys
Chris Hall
christopher.k.hall at gmail.com
Sat Apr 1 01:55:13 GMT 2006
models:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
beongs_to :user #
end
controller
class PostController < ApplicationController
def new
@post = Post.new
@users = User.find(:all)
end
end
view (new.rhtml)
<%= start_form_tag :action => "create" %>
...
<%= select :post, :user_id, options_from_collection_for_select(@users, "id",
"name") %>
...
<%= end_form_tag %>
On 3/31/06, Arch Stanton <d6veteran at gmail.com> wrote:
>
> Steve Koppelman wrote:
> > In this case, why won't it?
> >
> > In your user model, you'd say
> >
> > has_many :posts
> >
> > and in the post model you'd say
> >
> > belongs_to :user
> >
> > and that's all you need for what you describe. What's not working?
> >
>
>
> Well in the view I get no visibility to the foreign keys. Just now I
> found this example:
>
> class Student < ActiveRecord::Base
> set_table_name "students"
> set_primary_key "id"
>
> belongs_to :advisor, :class_name=>"Advisor", :foreign_key =>
> "advisor_id"
> belongs_to :second_advisor, :class_name=>"Advisor", :foreign_key =>
> "sub_advisor"
>
> has_and_belongs_to_many :courses, :class_name=>"Course"
>
> end
>
> I want my posts/edit to allow me to select a user.
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060401/58a959d5/attachment-0001.html
More information about the Rails
mailing list