0

こんにちは私は私のアプリの関係を通してhas_manyに少し問題があり、いくつかの助けを見つけることを望んでいました。だから私はユーザーと講義を持っています。講義は1人のユーザーによって作成されますが、他のユーザーは作成された講義に「参加」できます。ユーザーは、自分が作成した講義の独自のプロファイルフィードと、友人が作成した講義のフィードを持っています。ただし、この質問は講義を作成することではなく、すでに作成されている講義に「参加」することです。講義と参加したユーザー(私は「アクティブ」と呼びます)の間のこの関係を処理するために、「講義関係」モデルとコントローラーを作成しました。次に、ユーザーは講義を「終了」する必要があります(「終了」をクリックするか、ヘッダーナビゲーションリンクの1つに移動します)。

私が持っているもの: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

レクチャーリレーションシップ_コントローラー.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 %>

必要になる可能性のあるその他のファイル:

config / routers.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+\-.]+@[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

助けてくれてありがとう-私はしばらくここにいるので、前述のように、私と一緒に私の問題を解決する時間があるかもしれない誰かに大いに感謝します...

4

2 に答える 2

2

メンバーシップテーブルに登録を使用して、次のモデルから始めます。
これには、クラスの命名が重要です。

class User < ActiveRecord::Base
  has_many :enrollments
  has_many :users, :through => :enrollments
end

class Lecture < ActiveRecord::Base
  has_many enrollments
  has_many users, :through => :enrollments
end

class Enrollment < ActiveRecord::Base
  belongs_to :user
  belongs_to :lecture
end

Railsの主な秘訣は、正しい名前を使用し、規則に従い、フレームワークにほとんどの作業を任せることです。パスを降りると、エラーが無限に続く可能性があり、コードがすぐに乱雑になります:)

于 2012-06-08T22:36:22.120 に答える
1

_join.html.erbがbuild新しいlecturerelationships関連付けを形成するために使用し、次にjoin!呼び出しcreateが同じオブジェクト上に別の関連付けを作成するために使用するようです。私の推測では、2番目の呼び出しは、最初の呼び出しによって開始されたトランザクションを中断します。そのため、database is locked: commit transactionエラーが発生します。

この種のコードをあなたの見解から遠ざけることは良い習慣だと思います。

于 2012-06-08T22:14:07.357 に答える