English 中文(简体)
铁路模型数据不明
原标题:Filtering rails model with unknown data

我对一个特定用户的开放式桶名单表示赞扬,该名单附有以下代码。

@open_buckets = Buckets.order("buckets.id ASC").where(:user_id => @user.id)

还有一个用户专用桶的概念。 我只想在用户是桶顶板时归还私人桶。 私人领域是 b。 Can 我在某种程度上改变了我的问候,以便所有用户都能获得公共桶,只有所有者获得私人桶和公共桶?

我所想的唯一事情是检查是否与正在要求使用桶的用户相同,并作不同的询问。 如下。

if :session["user_id"] => :user_id
    @open_buckets = Buckets.order("buckets.id ASC").where(:user_id => @user_id)
else
    @open_buckets = Buckets.order("buckets.id ASC").where(:user_id => @user_id).where(:private => true)

增 编

最佳回答

确实,尽管我不同意使用一种观点(或一种清单/部分,更具体地说)来显示两种不同类型的信息,但这里是你可以做的:

@buckets = Buckets.order("buckets.id ASC").where(:user_id => @user_id, :private => false)
@buckets += @buckets.where(:private => true) if current_user.id == user.id

你们还可以创造一些冷却空间,在模型中处理这种半复合逻辑,如果是的话,并更充分地利用你的协会:

@user = User.find(params[:user_id])
@buckets = @user.buckets.public
@buckets += @user.buckets.private if @current_user == @user
respond_with @buckets = @buckets.order("buckets.id asc")

and the Buckets model:

scope :public, where(:private => false)
scope :private, where(:private => true)

作为附带说明,示范类名称始终应是独一无二的。 你的榜样是<条码>Buckets,这样,如果不是打字,那么我建议如果这样做是实际的,就予以回击和改变。 这将进一步造成混乱,特别是未来开发商申请。

问题回答

www.un.org/chinese/ga/president

class Bucket
  ...

  named_scope :visible_buckets_for, lambda {|user| { :conditions=>["private =  f  OR (private =  t  and user_id = ?)", current_user.id], :order=>"buckets.id ASC" } }

如果目前的用户是用户必须检索目前登录在用户中的<条码>id的任何方法,<条码>t 是亚洲开发银行在<条码>上的具体表述。 一些非银使用“真实”或非零号。

然后,请你说:

 > Bucket.visible_buckets_for(current_user)
=> [<#Bucket1>, <#Bucket2>, <#Bucket3> ... ]

......系一系列的<代码>Bucket物体,按<代码>id分类。

You can pass any user to that named scope, in case you need to find this out for more than just the current user.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签