Ruby on rails Rails有很多条件
我需要在很多这样的关系中使用:Ruby on rails Rails有很多条件,ruby-on-rails,will-paginate,Ruby On Rails,Will Paginate,我需要在很多这样的关系中使用: c = "(f.profile_id = #{self.id} OR f.friend_id = #{self.id})" c += AND + "(CASE WHEN f.profile_id=#{self.id} THEN f.friend_id ELSE f.profile_id END = p.id)" c += AND + "(CASE WHEN f.profile_id=#{self.id} THEN f.profile_rej
c = "(f.profile_id = #{self.id} OR f.friend_id = #{self.id})"
c += AND + "(CASE WHEN f.profile_id=#{self.id} THEN f.friend_id ELSE f.profile_id END = p.id)"
c += AND + "(CASE WHEN f.profile_id=#{self.id} THEN f.profile_rejected ELSE f.friend_rejected END = 1)"
c += AND + "(p.banned = 0)"
如何在那里设置self.id?,或者如何将id传递给那里?
然后我想使用will_paginate插件:
has_many :removed_friends, :conditions => ???
谢谢你的帮助
编辑:
使用单引号将条件括起来:
class Profile < ActiveRecord::Base
has_many :friendships
has_many :removed_friends, :class_name => 'Profile', :through => :friendships, :conditions =>
"(friendships.profile_id = #{self.id} OR friendships.friend_id = #{self.id})"
"AND (CASE WHEN friendships.profile_id=#{self.id} THEN friendships.profile_rejected ELSE friendships.friend_rejected END = 1)" +
"AND (p.banned = 0)"
end
class Friendship < ActiveRecord::Base
belongs_to :profile
belongs_to :removed_friend, :class_name => 'Profile', :foreign_key => "(CASE WHEN friendships.profile_id = #{self.id} THEN friend_id ELSE profile_id END)"
end
使用单引号将条件括起来:
class Profile < ActiveRecord::Base
has_many :friendships
has_many :removed_friends, :class_name => 'Profile', :through => :friendships, :conditions =>
"(friendships.profile_id = #{self.id} OR friendships.friend_id = #{self.id})"
"AND (CASE WHEN friendships.profile_id=#{self.id} THEN friendships.profile_rejected ELSE friendships.friend_rejected END = 1)" +
"AND (p.banned = 0)"
end
class Friendship < ActiveRecord::Base
belongs_to :profile
belongs_to :removed_friend, :class_name => 'Profile', :foreign_key => "(CASE WHEN friendships.profile_id = #{self.id} THEN friend_id ELSE profile_id END)"
end
您可能希望将其分解为一系列命名作用域,这些作用域可以分阶段应用,而不是一次性应用。例如,提取禁用部分:
class Profile < ActiveRecord::Base
has_many :friendships
has_many :removed_friends, :class_name => 'Profile', :through => :friendships,
:conditions => '
( friendships.profile_id = #{self.id} OR
friendships.friend_id = #{self.id}
) AND
(CASE WHEN friendships.profile_id=#{self.id}
THEN friendships.profile_rejected
ELSE friendships.friend_rejected
END = 1
) AND
(p.banned = 0)'
end
在人际关系中使用繁重的条件必然会带来麻烦。如果可能的话,可以尝试对表进行非规范化,创建具有简单数据版本的派生列,或者通过其他方式使查询变得更容易。您可能希望将其分解为一系列命名范围,这些范围可以分阶段应用,而不是一次全部应用。例如,提取禁用部分:
class Profile < ActiveRecord::Base
has_many :friendships
has_many :removed_friends, :class_name => 'Profile', :through => :friendships,
:conditions => '
( friendships.profile_id = #{self.id} OR
friendships.friend_id = #{self.id}
) AND
(CASE WHEN friendships.profile_id=#{self.id}
THEN friendships.profile_rejected
ELSE friendships.friend_rejected
END = 1
) AND
(p.banned = 0)'
end
在人际关系中使用繁重的条件必然会带来麻烦。如果可能的话,可以尝试对表进行非规范化,创建具有数据的简单版本的派生列,或者通过其他方式使查询更容易。这里确实有两种关系。你有: 被拒绝的来自侧面的友谊 朋友一方拒绝的友谊 我不知道为什么双方都可以拒绝友谊,也许你需要看看你的模型,看看哪一方要求友谊?最好是认为请求者取消了请求而不是说它被拒绝了?p> 无论如何,我会将其建模为两个独立的关系:
class Friend < ActiveRecord::Base
named_scope :banned, lambda { |*banned| {
:conditions => { :banned => banned.empty? ? 1 : (banned.first ? 1 : 0) }
}}
end
@profile.friends.removed.banned(false).paginate(:page => 1, :per_page => 20)
这有点不受欢迎,因为removed_friends不再是一种关系,所以你不能再像Profile.removed_friends.find:all,:conditions=>{:name=>bleh}这样做了,但这是一个相当复杂的情况。这种情况相当复杂 你在这里真的有两种关系。你有: 被拒绝的来自侧面的友谊 朋友一方拒绝的友谊 我不知道为什么双方都可以拒绝友谊,也许你需要看看你的模型,看看哪一方要求友谊?最好是认为请求者取消了请求而不是说它被拒绝了?p> 无论如何,我会将其建模为两个独立的关系:
class Friend < ActiveRecord::Base
named_scope :banned, lambda { |*banned| {
:conditions => { :banned => banned.empty? ? 1 : (banned.first ? 1 : 0) }
}}
end
@profile.friends.removed.banned(false).paginate(:page => 1, :per_page => 20)
这有点不受欢迎,因为removed_friends不再是一种关系,所以你不能再像Profile.removed_friends.find:all,:conditions=>{:name=>bleh}这样做了,但这是一个相当复杂的情况。这种情况相当复杂 这不是问题所在。我会告诉你我想做什么。我将编辑我的上一个问题。这不是问题所在。我会告诉你我想做什么。我将编辑我的上一个问题。在我的数据库中是这样的:profile_id是发出请求的,friend_id是接收朋友请求的,然后有一个profile_拒绝最初为false,但是如果profile_id拒绝,友谊将设置为true。然后是一个被拒绝的朋友,那是假的。然后是一个被接受的谎言,在朋友接受后,友谊才是真的。工会…+。。。这真的很糟糕,因为我不能提出排序,限制和抵消的要求,以良好的工作。无论如何谢谢你的时间,很快我会更好地向你解释我的意思。我很快就会回来…在我的数据库中是这样的:profile_id,是发出请求的那个,friend_id,是接收朋友请求的那个,然后有一个profile_拒绝,最初是假的,但是如果profile_id拒绝,友谊将被设置为true。然后是一个被拒绝的朋友,那是假的。然后是一个被接受的谎言,在朋友接受后,友谊才是真的。工会…+。。。这真的很糟糕,因为我不能提出排序,限制和抵消的要求,以良好的工作。无论如何谢谢你的时间,很快我会更好地向你解释我的意思。我很快就回来了…你必须使用单引号才能工作。我一直在使用它。这会让你面临SQL注入攻击吗?在那里看到{self.id}而没有参数绑定是令人担忧的。注意,rails 3.05及更高版本中不推荐使用这种技巧。你可以用proc或finder完成同样的事情_sql@JohnNaegle在这种情况下,SQL注入不太可能,因为代码正在替换模型属性,而不是用户输入。当然,黑客总是有可能设法在id字段中存储虚假SQL。我仅对id字段使用此方法。仅对id字段使用此方法是有意义的,我想对其他属性使用此方法。必须使用单引号才能使用此方法。我一直在使用它。这会让你面临SQL注入攻击吗?在没有参数绑定的情况下看到{self.id}是值得关注的
在rails 3.05及更高版本中,is技巧被弃用。你可以用proc或finder完成同样的事情_sql@JohnNaegle在这种情况下,SQL注入不太可能,因为代码正在替换模型属性,而不是用户输入。当然,黑客总是有可能设法在id字段中存储虚假SQL。我只对id字段使用这种方法。只对id字段使用它是有意义的,我想对其他属性使用它。