Tag: activerecord

批量将记录插入Active Record表

我发现我的Model.create! 当我一次添加大量记录时,语句需要很长时间才能运行。 看看ActiveRecord-Import但是它没有使用哈希数组(这是我拥有的,我认为这很常见)。 如何提高性能?

Rails与城市一起获得独特的国家

如何列出相关城市后面的唯一国家/地区? 以下是我的产品表: name country city p1 US New York p2 US Boston p3 US Chicago k1 UK London k2 UK Liverpool 控制器: @countries = Product.joins(:user).distinct.where(“country is not null and country ””).where(:users => {:merchant_status => 1}).pluck(:country) @cities = Product.joins(:user).distinct.where(“city is not null and city ””).where(:users => {:merchant_status => 1}).pluck(:city) @countries.map! {|country| country.split.map(&:capitalize).join(‘ ‘)} @search_location_country = @countries […]

使用rails嵌套模型来创建*外部对象并同时*编辑*现有的嵌套对象?

使用Rails 2.3.8 目标是创建一个Blogger,同时更新嵌套的User模型(如果信息已更改等),或者创建一个全新的用户(如果它尚不存在)。 模型: class Blogger < ActiveRecord::Base belongs_to :user accepts_nested_attributes_for :user end Blogger控制器: def new @blogger = Blogger.new if user = self.get_user_from_session @blogger.user = user else @blogger.build_user end # get_user_from_session returns existing user # saved in session (if there is one) end def create @blogger = Blogger.new(params[:blogger]) # … end 形成: # … other […]

无法访问块中的ActiveRecord mutators

我在一个Rails控制器中,我试图在一个块中访问我的实例变量:这给出了一个错误,说“没有方法field1 for Nil”: Prawn::Document.generate(“hello.pdf”) do @model.field1 end 但是,如果我这样做,那么它的工作原理: my_model = @model Prawn::Document.generate(“hello.pdf”) do my_model.field1 end 这可能与块中的ActiveRecord访问器或实例变量有关吗?

rails关联方法如何工作?

rails关联方法如何工作? 让我们考虑这个例子 class User < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :user end 现在我可以做点什么 @user = User.find(:first) @user.articles 这会抓取属于该用户的文章。 到现在为止还挺好。 现在我可以继续在某些条件下对这些文章进行查找。 @user.articles.find(:all, :conditions => {:sector_id => 3}) 或者简单地声明和关联方法 class User {:sector_id => sector_id}) end end end 并做 @user.articles.of_sector(3) 现在我的问题是,这是如何find使用关联方法获取的ActiveRecord对象数组? 因为如果我们实现我们自己的名为articles的User实例方法并编写我们自己的实现,它给出了与关联方法完全相同的结果,那么ActiveRecord对象的fetch数组上的find将无法工作。 我的猜测是关联方法将某些属性附加到获取对象数组,这使得能够使用find和其他ActiveRecord方法进一步查询。 在这种情况下,代码执行的顺序是什么? 我怎么能validation这个?

无法选择created_at分组的行

我有桌子: +—-+———————-+———————+ | id | implemented_features | created_at | +—-+———————-+———————+ | 1 | 19 | 2013-07-18 04:10:12 | | 2 | 6 | 2013-07-18 04:10:12 | | 3 | 26 | 2013-07-19 04:10:12 | | 4 | 11 | 2013-07-19 04:10:12 | | 5 | 1 | 2013-07-20 04:10:12 | +—-+———————-+———————+ 当我通过My​​SQL直接查询它时,它可以完美地工作 select date(created_at) as […]

作用域查询中的ActiveRecord OR子句

statuses = %w(sick healthy hungry) query = User.scoped(:joins => ‘left outer join pets on pets.user_id = users.id’, :conditions => { ‘pets.id’ => nil, ‘users.job_status’ => stati }) 鉴于上面的代码,是否可以在:conditions添加一个OR子句来表示类似的东西 where (pets.id is NULL AND users.status IN (‘sick’, ‘healthy’, ‘hungry’)) OR (users.gender = ‘male’)

ActiveRecord:如何克隆嵌套关联?

我目前正在克隆这样的单级关联: class Survey < ActiveRecord::Base def duplicate new_template = self.clone new_template.questions << self.questions.collect { |question| question.clone } new_template.save end end 因此, Survey克隆然后克隆与该调查相关的Questions 。 精细。 这非常有效。 但我遇到的问题是每个问题都有很多问题。 所以Survey has_many Questions which has_many Answers 。 我无法弄清楚如何正确克隆答案。 我试过这个: def duplicate new_template = self.clone self.questions.each do |question| new_question = question.clone new_question.save question.answers.each do |answer| new_answer = answer.clone new_answer.save new_question.answers […]

所有在数组中的PostgreSQL

实现子句中最简单,最快速的方法是什么,必须匹配数组中的所有元素 – 使用IN时不仅仅是一个? 毕竟它应该像mongodb的$ all 。 考虑到conversation_users是conversation_id和user_id之间的连接表的群组对话我有类似这样的想法: WHERE (conversations_users.user_id ALL IN (1,2)) 更新 16.07.12 添加有关架构和案例的更多信息: join-table非常简单: Table “public.conversations_users” Column | Type | Modifiers | Storage | Description —————–+———+———–+———+————- conversation_id | integer | | plain | user_id | integer | | plain | 对话有很多用户,用户属于许多对话。 为了找到对话中的所有用户,我正在使用此连接表。 最后,我试图找出一个ruby on rails scope ,根据它的参与者找到我的对话 – 例如: scope :between, ->(*users) { […]

GroupingError:ERROR:列必须出现在GROUP BY子句中或用于聚合函数

我的控制器中的代码是按最高平均评价等级排名的专辑(此解决方案中使用的代码如何通过has_many评论关系显示评分最高的专辑 ): @albums = Album.joins(:reviews).select(“*, avg(reviews.rating) as average_rating”).group(“albums.id”).order(“average_rating DESC”) 这段代码在我的开发环境(sqlite3)中运行得很好,但是当我将代码推送到heroku和postgresql时,我遇到了这个错误: PG::GroupingError: ERROR: column “reviews.id” must appear in the GROUP BY clause or be used in an aggregate function 我意识到这是一个相当普遍的问题,我对SQL有点缺乏经验,所以我在重构代码时遇到了麻烦,因此它可以在我的开发和生产环境中工作。