嗨,我通過關係爲我的應用has_many有點麻煩,並希望找到一些幫助。所以我有用戶&講座。講座由一個用戶創建,但其他用戶則可以「加入」已創建的講座。用戶擁有他們自己創建的講座的簡介Feed &也有朋友創建的講座摘要。然而,這個問題不是要創建一個講座,而是要「加入」已經創建的講座。我創建了一個「講座關係」模型&控制器來處理已加入的用戶(我稱其爲「活動人員」)的講座&之間的這種關係。用戶還必須「退出」講座(通過單擊「退出」或導航到其中一個標題導航鏈接)。我很感激,如果任何人都可以通過一些這和我一起工作......ruby on rails has_many通過關係
我有: users.rb的模型 Lectures.rb模型 Users_controller Lectures_controller
那麼下面的模型
lecturerelationship.rb
class lecturerelationship < ActiveRecord::Base
attr_accessible :active_id, :joinedlecture_id
belongs_to :active, :class_name => "User"
belongs_to :joinedlecture, :class_name => "Lecture"
validates :active_id, :presence => true
validates :joinedlecture_id, :presence => true
end
lecturerelationships_controller.rb
已創建(通過朋友)顯示在用戶210個class LecturerelationshipsController < ApplicationController
before_filter :signed_in_user
def create
@lecture = Lecture.find(params[:lecturerelationship][:joinedlecture_id])
current_user.join!(@lecture)
redirect_to @lecture
end
def destroy
@lecture = Lecturerelationship.find(params[:id]).joinedlecture
current_user.exit!(@user)
redirect_to @user
end
end
講座飼料中的以下文件
_activity_item.html.erb
<li id="<%= activity_item.id %>">
<%= link_to gravatar_for(activity_item.user, :size => 200), activity_item.user %><br clear="all">
<%= render :partial => 'shared/join', :locals => {:activity_item => activity_item} %>
<span class="title"><%= link_to activity_item.title, lecture_url(activity_item) %></span><br clear="all">
<span class="user">
Joined by <%= link_to activity_item.user.name, activity_item.user %>
</span><br clear="all">
<span class="timestamp">
<%= time_ago_in_words(activity_item.created_at) %> ago.
</span>
<% if current_user?(activity_item.user) %>
<%= link_to "delete", activity_item, :method => :delete,
:confirm => "Are you sure?",
:title => activity_item.content %>
<% end %>
</li>
然後你就看到我鏈接到了「共享/加入部分高於其可以在下面
_join.html.erb文件中可以看出
<%= form_for(current_user.lecturerelationships.build(:joinedlecture_id => activity_item.id)) do |f| %>
<div>
<%= f.hidden_field :joinedlecture_id %>
</div>
<%= f.submit "Join", :class => "btn btn-large btn-info" %>
<% end %>
個
,可能需要一些更多的文件:
的config/routes.rb中
SampleApp::Application.routes.draw do
resources :users do
member do
get :following, :followers, :joined_lectures
end
end
resources :sessions, :only => [:new, :create, :destroy]
resources :lectures, :only => [:create, :destroy, :show]
resources :relationships, :only => [:create, :destroy] #for users following each other
resources :lecturerelationships, :only => [:create, :destroy] #users joining existing lectures
所以會發生什麼是講座來到我activity_feed與底部的加入按鈕選項...這應該創建一個「活躍」的講座關係&「加入講座」(顯然應該是來自用戶&講座班。但我得到的錯誤,當我點擊加入按鈕,如下:
ActiveRecord::StatementInvalid in LecturerelationshipsController#create
SQLite3::ConstraintException: constraint failed: INSERT INTO "lecturerelationships" ("active_id", "created_at", "joinedlecture_id", "updated_at") VALUES (?, ?, ?, ?)
此外,我已經包括了我的用戶模型(好像錯誤是指它) user.rb
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :lectures, :dependent => :destroy
has_many :lecturerelationships, :foreign_key => "active_id", :dependent => :destroy
has_many :joined_lectures, :through => :lecturerelationships, :source => :joinedlecture
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, :presence => true, :length => { :maximum => 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true,
:format => { :with => VALID_EMAIL_REGEX },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true, :length => { :minimum => 6 }
validates :password_confirmation, :presence => true
def activity
# This feed is for "My Activity" - basically lectures i've started
Lecture.where("user_id = ?", id)
end
def friendactivity
Lecture.from_users_followed_by(self)
end
# lECTURE TO USER (JOINING) RELATIONSHIPS
def joined?(selected_lecture)
lecturerelationships.find_by_joinedlecture_id(selected_lecture.id)
end
def join!(selected_lecture)
lecturerelationships.create!(:joinedlecture_id => selected_lecture.id)
end
def exit!(selected_lecture)
lecturerelationships.find_by_joinedlecture_id(selected_lecture.id).destroy
end
end
感謝任何和所有的幫助 - 我會在這裏一段時間,所以提到我非常感謝有人可能有時間來解決我的問題與我...
對於其中一種,您可能會用更短的變量名稱獲得更愉悅的導航體驗。一個大問題是你的創建操作應該從Lecture表中檢索數據,而不是用戶表。 – cdesrosiers
謝謝cdesrosiers - 這是我完全忽略的東西。我已經更新了上面的那部分。但現在我收到一個新的錯誤,我現在添加 – BennyB
正確的約旦,我試圖創建一個joinedlecture_id(基於常規講座類),有很多「actives_id」(已加入講座的用戶)。 – BennyB