arel

How can I create a scope in a model that returns only those objects with an associated rank greater than the current_user's rank?

China☆狼群 提交于 2019-12-08 08:38:47
I am trying to create a :readable scope in my Page model to return all the Pages that the current_person has sufficient 'rank' to read: scope :readable, lambda { |current_person| joins(:role_readable) .where(:role_readable[:rank].gte(current_person.roles.first.rank) ) } I've tried many scope permutations, including this one, with no success (the one above gives a "can't convert Symbol into Integer" error). The problem is made more complex because Users (which handle authentication etc. / synonymous with accounts) have_many People , which represent the User 's presence in an organization — ie.

How can I create a scope in a model that returns only those objects with an associated rank greater than the current_user's rank?

杀马特。学长 韩版系。学妹 提交于 2019-12-08 07:09:06
问题 I am trying to create a :readable scope in my Page model to return all the Pages that the current_person has sufficient 'rank' to read: scope :readable, lambda { |current_person| joins(:role_readable) .where(:role_readable[:rank].gte(current_person.roles.first.rank) ) } I've tried many scope permutations, including this one, with no success (the one above gives a "can't convert Symbol into Integer" error). The problem is made more complex because Users (which handle authentication etc. /

Rails gem Ransack -> search 'or condition' with 'is null' and a specific value (field=x OR field IS NULL)

狂风中的少年 提交于 2019-12-07 14:45:45
问题 I made a support ticket system for our supporters - programmed with ruby on rails (Ruby 1.9.3 Rails 3.2) There is a ticket model with a belongs_to association to users (supporter). I use Ernie's gem Ransack for searching. When the supporter searches his own tickets (handled by tickets_controller.index) he should also see the unassociated tickets in the search result (views/tickets/index) (in combination with the other serach conditions eg. "date" or something else) (So that he can take an

How to get the arel table of a habtm association?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 06:33:33
问题 I have two ActiveRecord models which have a HABTM association. I want to write a scope to get the orphan records using Arel. My problem is that I couldn't find a method to retrieve the arel_table of the association. Since the relation is HABTM, there is no model to call arel_table on. I have the following now (which works), but I make a new arel table with the name of the join table (retrieved by using the reflect_on_association method). scope :orphans, lambda { teachers = arel_table join

Rails gem Ransack -> search 'or condition' with 'is null' and a specific value (field=x OR field IS NULL)

此生再无相见时 提交于 2019-12-06 02:37:20
I made a support ticket system for our supporters - programmed with ruby on rails (Ruby 1.9.3 Rails 3.2) There is a ticket model with a belongs_to association to users (supporter). I use Ernie's gem Ransack for searching. When the supporter searches his own tickets (handled by tickets_controller.index) he should also see the unassociated tickets in the search result (views/tickets/index) (in combination with the other serach conditions eg. "date" or something else) (So that he can take an open ticket) expected SQL statement: SELECT * FROM tickets WHERE ... AND (user_id=5 OR user_id IS NULL)

Finding unique records, ordered by field in association, with PostgreSQL and Rails 3?

╄→гoц情女王★ 提交于 2019-12-06 01:34:32
UPDATE : So thanks to @Erwin Brandstetter, I now have this: def self.unique_users_by_company(company) users = User.arel_table cards = Card.arel_table users_columns = User.column_names.map { |col| users[col.to_sym] } cards_condition = cards[:company_id].eq(company.id). and(cards[:user_id].eq(users[:id])) User.joins(:cards).where(cards_condition).group(users_columns). order('min(cards.created_at)') end ... which seems to do exactly what I want. There are two shortcomings that I would still like to have addressed, however: The order() clause is using straight SQL instead of Arel (couldn't figure

shouldn't `where.not(field: “something”)` include `where(field: nil)`?

你说的曾经没有我的故事 提交于 2019-12-05 19:26:58
Perhaps I'm going crazy, or just need a break, but in my rails console Order.where(state: nil).count returns 1010 , but Order.where.not(state: "pending").count returns 0 ... If an order's state is nil, then it is not "pending", so I expect the set returned by not(state: "pending") to include the set where(state: nil) . Does arel not work this way? If not, does arel work a different way? EDIT: more info! When I go to another database, where some records have a state other than nil, and I run Order.where.not(state: "pending").count I get back a bunch of orders, none of which are "pending" but

How to get the arel table of a habtm association?

拥有回忆 提交于 2019-12-05 11:34:40
I have two ActiveRecord models which have a HABTM association. I want to write a scope to get the orphan records using Arel. My problem is that I couldn't find a method to retrieve the arel_table of the association. Since the relation is HABTM, there is no model to call arel_table on. I have the following now (which works), but I make a new arel table with the name of the join table (retrieved by using the reflect_on_association method). scope :orphans, lambda { teachers = arel_table join_table = Arel::Table.new(reflect_on_association(:groups).options[:join_table]) join_table_condition = join

Best way to find a single record using ActiveRecord 3 / Arel?

让人想犯罪 __ 提交于 2019-12-05 08:28:38
问题 Where I used to do this: Foo.find_by_bar('a-value') I can now do this: Foo.where(:bar => 'a-value').limit(1).first Is this recommended? Is this the best way? Should I continue to use the "old" way because it continues to be useful syntactic sugar, or is there an Even Better way I can do that now, which will support chaining and all the other good stuff? 回答1: Rails 4 : Foo.find_by bar: 'a_value' , wibble: 'a wibble value' 回答2: I think the preferable way to return a single record would be along

Writing “not in” sql query using AREL

送分小仙女□ 提交于 2019-12-05 07:29:19
I have a scope defined as follows: scope :ignore_unavailable, lambda { where([ "Item.id NOT IN (SELECT id FROM Cars WHERE Cars.status = 'NA'" ]) } Currently its using hardcoded tables names. How can I improve it using frameworks like Arel ? Will appreciate any help here. I am on Rails 3.2 If you are using rails 4, one way would be scope :ignore_unavailable, lambda { where.not(id: Car.where(:status => "NA").pluck(:id)) } For rails 3 scope :ignore_unavailable, lambda { where("id not in (?)", Car.where(:status => "NA").pluck(:id)) } Since the task description asks for an answer using AREL, I