scopes

Why using merge method with scopes isn't working anymore on Rails 3.1?

半腔热情 提交于 2019-12-06 20:57:56
问题 I stumbled upon a wonderful article about scopes on Rails 3+ : http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html You can read there (in 'Crazy Town' section) that it's possible to merge scopes from different models like this : class User < ActiveRecord::Base scope :published, lambda { joins(:posts).group("users.id") & Post.published } end which works just as expected, and allows you to do : User.published.to_sql #=> SELECT

Laravel Global Scope Auth

≯℡__Kan透↙ 提交于 2019-12-06 06:20:29
问题 I've created an anonimus global scope in the users model as below in order to get only public users in the frontend: protected static function boot() { parent::boot(); static::addGlobalScope('is_public', function(Builder $builder) { $builder->where('is_public', '=', 1); }); } But... when i need to perform the login in the backend i need of course to check for not-public users so i need to exclude the global scope. Is this possibile using the default AuthController of laravel? Many Thanks!!

How can I use scopes with Ransack in Rails 3?

女生的网名这么多〃 提交于 2019-12-06 02:58:04
问题 In my Widget model I have the following: scope :accessible_to, lambda { |user| if user.has_role?('admin') self.all else roles = user.roles role_ids = [] roles.each { |r| role_ids << r.id } self.joins(:widget_assignments).where('widget_assignments.role_id' => role_ids) end } Ideally, I would like to use this scope as a filter for Ransack's search results, so in my controller I have: def index @q = Widget.accessible_to(current_user).search(params[:q]) @widgets = @q.result.order('created_at DESC

Find_by_sql as a Rails scope

∥☆過路亽.° 提交于 2019-12-06 02:43:38
问题 r937 from Sitepoint was kind enough to help me figure out the query I need to return correct results from my database. What I need is to be able to use this query as a scope and to be able to chain other scopes onto this one. The query is: SELECT coasters.* FROM ( SELECT order_ridden, MAX(version) AS max_version FROM coasters GROUP BY order_ridden ) AS m INNER JOIN coasters ON coasters.order_ridden = m.order_ridden AND COALESCE(coasters.version,0) = COALESCE(m.max_version,0) I tried making a

Why using merge method with scopes isn't working anymore on Rails 3.1?

早过忘川 提交于 2019-12-05 01:09:08
I stumbled upon a wonderful article about scopes on Rails 3+ : http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html You can read there (in 'Crazy Town' section) that it's possible to merge scopes from different models like this : class User < ActiveRecord::Base scope :published, lambda { joins(:posts).group("users.id") & Post.published } end which works just as expected, and allows you to do : User.published.to_sql #=> SELECT users.* FROM "users" # INNER JOIN "posts" ON "posts"."author_id" = "users"."id" # WHERE (posts

Laravel Global Scope Auth

隐身守侯 提交于 2019-12-04 09:45:23
I've created an anonimus global scope in the users model as below in order to get only public users in the frontend: protected static function boot() { parent::boot(); static::addGlobalScope('is_public', function(Builder $builder) { $builder->where('is_public', '=', 1); }); } But... when i need to perform the login in the backend i need of course to check for not-public users so i need to exclude the global scope. Is this possibile using the default AuthController of laravel? Many Thanks!! You just need to create two Models - one without global scope (i.e. AuthUser) and another with the global

How to write scope with belongs_to object?

ぃ、小莉子 提交于 2019-12-03 12:36:53
问题 I have the following models Models Job belongs_to :company Company has_many :jobs Right now I select all the Jobs which have an accepted Company using the following method: def self.with_accepted_company Job.all.reject {|job| job.company.state != "accepted" } end But I would like to use scope for that and use it with other scopes. Is this possible to write that scope in the Job model? 回答1: I would do something like this (from http://guides.rubyonrails.org/active_record_querying.html) class

Autowiring request scoped beans into application scoped beans

孤街浪徒 提交于 2019-12-03 07:08:11
Is it possible to autowire a request scoped bean into an application scoped bean. i.e I have a class RequestScopedBean: class RequestScopedBean { .... .... .... } and a class Application scoped bean in which the request scoped bean is autowired. class ApplicationScopedBean { @Autowire private RequestScopedBean requestScopedBean; .... .... .... } and the spring-config xml is as follows: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi

How to write scope with belongs_to object?

落花浮王杯 提交于 2019-12-03 03:13:44
I have the following models Models Job belongs_to :company Company has_many :jobs Right now I select all the Jobs which have an accepted Company using the following method: def self.with_accepted_company Job.all.reject {|job| job.company.state != "accepted" } end But I would like to use scope for that and use it with other scopes. Is this possible to write that scope in the Job model? I would do something like this (from http://guides.rubyonrails.org/active_record_querying.html ) class Job scope :accepted_with_active_company, ->() { joins(:company).where('companies.state' => "accepted") } end

Python modules - Scopes

橙三吉。 提交于 2019-12-02 09:18:59
I found what I think is peculiar behaviour by python 3.4. I made these small files to illustrate the problem I am facing. To focus on the problem, I now have a python program(module), peculiar.py, containing a class, and some functions. One of the functions are instantiating points from the class Point. When I left click the pygame window, a point is instantiated from the class Point, by this function, add_point. In the command window the instances created this way is listed. Everything is ok with this. As we can see the points crated are added to the points_list and listed in the command