Ruby on rails Mysql2::错误:未知列';友谊。地位';在';其中第'条;
首先,有一个问题,它几乎与我的问题相似,但我无法使它适用于我的代码。所以,我决定分开问 这就是我得到错误的地方:Ruby on rails Mysql2::错误:未知列';友谊。地位';在';其中第'条;,ruby-on-rails,Ruby On Rails,首先,有一个问题,它几乎与我的问题相似,但我无法使它适用于我的代码。所以,我决定分开问 这就是我得到错误的地方: has_many :friendships has_many :friends, :through => :friendships, :conditions => "status = 'accepted'", :order => :screen_name has_many :request
has_many :friendships
has_many :friends,
:through => :friendships,
:conditions => "status = 'accepted'",
:order => :screen_name
has_many :requested_friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'requested'",
:order => :created_at
has_many :pending_friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'pending'",
:order => :created_at
我不知道我是否应该分享一些我的代码来帮助你理解。如果你需要一些其他的部分,我也可以粘贴它们
这是全部错误:
Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table, :foreign_type
多谢各位
编辑
我解决了这个问题:
has_many :friendships
has_many :friends, -> { where(friendship: {status: 'accepted'}).order('created_at') }, :through => :friendships
has_many :requested_friends, -> { where(friendship: {status: 'requested'}).order('created_at') }, :through => :friendships, :source => :friend
has_many :pending_friends, -> { where(friendship: {status: 'pending'}).order('created_at') }, :through => :friendships, :source => :friend
这就是我现在遇到的错误:
Mysql2::Error: Unknown column 'friendship.status' in 'where clause': SELECT 1 AS one FROM `users` INNER JOIN `friendships` ON `users`.`id` = `friendships`.`friend_id` WHERE `friendships`.`user_id` = 6 AND `friendship`.`status` = 'requested' AND `users`.`id` = 8 LIMIT 1
这是发生错误的地方:
def accept # accept_request
if @user.requested_friends.include?(@friend)
Friendship.accept_request(@user, @friend)
end
redirect_to profile_path(params[:id])
end
具体而言:
if @user.requested_friends.include?(@friend)
接受您的请求:
def self.accept_request(user, friend)
transaction do
accept_one_side(user, friend)
accept_one_side(friend, user)
end
end
接受一方:
private
def self.accept_one_side(user, friend)
request = find_by_user_id_and_friend_id(user, friend)
request.status = 'accepted'
request.save!
end
问题已修复 问题出在很多方面 这是正确的版本:
has_many :friendships
has_many :friends, -> { where(friendships: {status: 'accepted'}).order('created_at') }, :through => :friendships
has_many :requested_friends, -> { where(friendships: {status: 'requested'}).order('created_at') }, :through => :friendships, :source => :friend
has_many :pending_friends, -> { where(friendships: {status: 'pending'}).order('created_at') }, :through => :friendships, :source => :friend
它可能看起来与上面的代码相同,但实际上并非如此
因为在第一段代码中,我写道:“where(友谊):,但它应该是:“where(友谊):”因此,我忘了放置's'的可能重复项根据您给我的链接,我修复了问题。但是现在我遇到了一些其他错误。看起来您试图从关联模型调用一个列,这需要一个联接。您能将错误发生的代码发布到哪里吗?@RyanK可能有很多错误,但仍然无法计算您需要的是a。可能类似于:
加入(:友谊)。where(友谊:{status:'accepted'}}
。注意where子句中的单数友谊。