Witam wszystkich,
Pisze sobie aplikację i doszedłem do momentu w którym muszę napisać Forum dyskusyjne.
Teraz sprawa wygląda tak, że mam 3 tabele:
[code] create_table “forum_categories”, :force => true do |t|
t.string “title”
t.text “text”
t.integer “position”
t.integer “mode_private”, :limit => 1, :default => 0
t.datetime “created_at”
t.datetime “updated_at”
end
create_table “forum_posts”, :force => true do |t|
t.text “text”
t.integer “user_id”
t.integer “thread”
t.datetime “created_at”
t.datetime “updated_at”
end
create_table “forum_threads”, :force => true do |t|
t.string “title”
t.integer “parent_id”
t.datetime “created_at”
t.datetime “updated_at”
end[/code]
Dokładnie jak to wygląda (relacje) przedstawiam Wam model:
[code]class ForumCategory < ActiveRecord::Base
has_many :forum_threads, :foreign_key => ‘parent_id’, :dependent => :destroy
validates_length_of :title, :within => 4…32, :message => ‘Długość kategorii musi mieć od 4 do 32 znaków’
acts_as_list
def to_param
“#{id},#{title.parameterize}”
end
end[/code]
class User < ActiveRecord::Base
has_many :forum_posts
...
[code]class ForumThread < ActiveRecord::Base
belongs_to :forum_category
belongs_to :user, :foreign_key => ‘user_id’
has_many :forum_posts, :foreign_key => ‘thread’, :dependent => :destroy
validates_length_of :title, :within => 5…32, :message => ‘Temat musi mieć od 5 do 32 znaków’
def post=(post)
forum_posts.build(post)
end
def to_param
“#{id},#{title.parameterize}”
end
end[/code]
class ForumPost < ActiveRecord::Base
belongs_to :forum_thread
belongs_to :user
validates_length_of :text, :within => 5..1024, :message => 'Treść musi mieć conajmniej 5 znaków'
end
Teraz mam pytanie, mianowicie, dlaczego nie user_id nie zawiera joina do tabeli usera, tylko samo id? wpisując przykładowo <%= post.user_id.login %> otrzymuję komunikat, że nie login == nil.
Drugie pytanie to, w jaki sposób przekazać przez formularz, id usera przy tworzeniu topicu? Dodam, że temat tworzę w ten sposób:
[code]<% title ‘Dodaj nowy temat’ %>
<% form_for @topic, :url => topic_insert_forum_url do |form| %>
<%= form.label :title, 'Tytuł' %> |
<%= form.text_field :title %>
<%= error_message_on @topic, :title %>
|
<%= form.label :text, 'Treść wpisu' %> |
<%= form.text_area :text %>
<%= error_message_on thread, :text %>
<%= form.hidden_field :user_id, :value => current_user.id %> #-- nie chcę przekazywać tego z poziomu szablonu...
|
<%= form.submit 'Dalej' %>
|