相关疑难解决方法(0)

Rails活动记录查询与'exists'的关联

我正在开发一个允许会员进行调查的应用程序(会员与Response有一对多的关系).Response保存member_id,question_id及其答案.

调查全部或全部提交,因此如果该成员的响应表中有任何记录,则他们已完成调查.

我的问题是,如何重新编写下面的查询,以便它实际工作?在SQL中,这将是EXISTS关键字的主要候选者.

 def surveys_completed
    members.where(responses: !nil ).count
 end 
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

47
推荐指数
5
解决办法
3万
查看次数

名为not found的关联可能是rails association中的拼写错误的问题

这是我的控制器

@post = Post.joins(:customers).select("customers.*,posts.*").find params[:id]
Run Code Online (Sandbox Code Playgroud)

我的帖子模型

belongs_to :customer
Run Code Online (Sandbox Code Playgroud)

我的客户模特

has_many :posts
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

Association named 'customers' was not found on Post; perhaps you misspelled it?
Run Code Online (Sandbox Code Playgroud)

这是我的控制器输出:

Processing by PostsController#show as */*
  Parameters: {"id"=>"6"}
  Post Load (0.5ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", "6"]]
Completed 500 Internal Server Error in 113ms

ActiveRecord::ConfigurationError (Association named 'customers' was not found on Post; perhaps you misspelled it?):
  app/controllers/posts_controller.rb:16:in `show'
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3

38
推荐指数
1
解决办法
2万
查看次数

如何根据属于第一个模型的另一个模型的属性查询模型?

如果我有一个模型Person,has_many Vehicles每个模型都Vehicle可以是类型,car或者motorcycle我如何查询所有拥有汽车的人和所有拥有摩托车的人?

我不认为这些是正确的:

Person.joins(:vehicles).where(vehicle_type: 'auto')
Person.joins(:vehicles).where(vehicle_type: 'motorcycle')
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails active-record-query

7
推荐指数
1
解决办法
2310
查看次数

使用带有子模型计数的named_scope

我有一个有很多孩子的简单父对象.我正在试图弄清楚如何使用命名范围来带回具有特定数量的孩子的父母.

这可能吗?

class Foo < ActiveRecord::Base
    has_many :bars
    named_scope :with_no_bars, ... # count of bars == 0
    named_scope :with_one_bar, ... # count of bars == 1
    named_scope :with_more_than_one_bar, ... # count of bars > 1
end

class Bar < ActiveRecord::Base
    belongs_to :foo
end
Run Code Online (Sandbox Code Playgroud)

我希望能做类似的事情 Foo.with_one_bar

我可以在父类上编写类似这样的方法,但我宁愿拥有命名范围的强大功能

activerecord named-scope ruby-on-rails ruby-on-rails-2

4
推荐指数
2
解决办法
4321
查看次数

Rails:查找关系具有指定属性的所有用户

我有三个模型:

class User < ActiveRecord::Base
  has_many :rosterplayers
  has_many :rosters, -> { uniq } ,  :through => :rosterplayers
end

class Roster < ActiveRecord::Base
  has_many :rosterplayers
  has_many :users, -> { uniq }, through: :rosterplayers
end

class Rosterplayer < ActiveRecord::Base
  belongs_to :roster
  belongs_to :user
  validates :user_id, :uniqueness => { :scope => :roster_id }
end
Run Code Online (Sandbox Code Playgroud)

Rosterplayer 表包含三列:user_id,roster_id,pending(布尔值)

问题:给定一个名册,我将如何检索当前待处理的所有用户?

尝试:我的第一次尝试是遍历名册中的所有用户:

@team.rosters[0].users.each do |u|
  Rosterplayer.find_by(roster_id: rosters[0].id, user_id: u.id, pending: true)
end
Run Code Online (Sandbox Code Playgroud)

但我觉得有更好的方法来做到这一点。

ruby-on-rails

3
推荐指数
1
解决办法
418
查看次数

Rails范围过滤元素没有has_many关联元素

我有以下型号:

class Property < ActiveRecord::Base
    has_many :photos
    scope :no_photos, -> { where('properties.id NOT IN (SELECT DISTINCT(property_id) FROM photos)') }
end

class Photo < ActiveRecord::Base
    belongs_to :property
end
Run Code Online (Sandbox Code Playgroud)

我知道我的范围非常低效.我需要另一种方法来获取没有任何相关照片的属性.

有帮助吗?

ruby activerecord ruby-on-rails

3
推荐指数
1
解决办法
1498
查看次数

Rails 3,具有lambda条件的has_one/has_many

我的模特在这里:

class User < ActiveRecord::Base
  has_many :bookmarks
end

class Topic < ActiveRecord::Base
  has_many :bookmarks
end

class Bookmark < ActiveRecord::Base
  belongs_to :user
  belongs_to :topic
  attr_accessible :position
  validates_uniqueness_of :user_id, :scope => :topic_id
end
Run Code Online (Sandbox Code Playgroud)

我想topicscurrent_user相关的for 来获取所有内容bookmark.ATM,我这样做:

Topic.all.each do |t|
    bookmark = t.bookmarks.where(user_id: current_user.id).last
    puts bookmark.position if bookmark
    puts t.name
end
Run Code Online (Sandbox Code Playgroud)

这很难看并且做了太多的数据库查询.我想要这样的东西:

class Topic < ActiveRecord::Base
  has_one :bookmark, :conditions => lambda {|u| "bookmarks.user_id = #{u.id}"}
end

Topic.includes(:bookmark, current_user).all.each do |t| # this must also includes topics without bookmark …
Run Code Online (Sandbox Code Playgroud)

ruby lambda activerecord ruby-on-rails ruby-on-rails-3

2
推荐指数
1
解决办法
5115
查看次数

使用rails查找与嵌套关联的记录

我有以下型号:

class Book < ApplicationRecord
  has_many :chapters
end

class Chapter < ApplicationRecord
  belongs_to :book
  has_many :pages
end

class Page < ApplicationRecord
  belongs_to :chapter
  has_many :paragraphs
end

class Paragrpah < ApplicationRecord
  belongs_to :page
end
Run Code Online (Sandbox Code Playgroud)

现在我想得到一本特定书中所有段落的清单.就像是:

@parapgraphs = Paragraph.pages.chapters.book.where(:title => 'My Book')
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到:

undefined method 'chapters' for 'pages'
Run Code Online (Sandbox Code Playgroud)

我正在使用Rails 4.2和Ruby 2.2

ruby activerecord ruby-on-rails where-clause ruby-on-rails-4

2
推荐指数
1
解决办法
525
查看次数