嗨,我的应用程序的 has_many through 关系遇到了一点麻烦,希望能找到一些帮助。所以我有用户和讲座。讲座由一位用户创建,但随后其他用户可以“加入”已创建的讲座。用户有他们自己创建的讲座的个人资料提要,也有朋友创建的讲座的提要。然而,这个问题不是关于创建讲座,而是“加入”已经创建的讲座。我创建了一个“lecturerelationships”模型和控制器来处理 Lectures 和已加入的用户(我称之为“actives”)之间的这种关系。用户还必须“退出”讲座(通过单击“退出”或导航到标题导航链接之一)。
我有: Users.rb 模型 Lectures.rb 模型 Users_controller Lectures_controller
然后是以下模型
讲师关系.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
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 %>
可能需要的更多文件:
配置/路由.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中,底部有一个加入按钮选项......这应该创建一个“活动”和“joinedlecture”的讲座关系(显然应该来自用户和讲座课程。但是当我点击加入按钮时我得到的错误如下:
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+\-.]+@[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
感谢您提供的所有帮助 - 我会在这里待一段时间,所以如前所述,我非常感谢可能有时间与我一起解决问题的人......