Tag: activerecord

是否可以在Rails中为无模型表使用’activerecord-import’gem。

我想将记录批量插入到没有Model的表中。 我确实按照链接如何在Rails 3中实现批量插入 ..一切都很好,除了’import’命令。 因为我没有模特。 我无法为该表创建一个空模型。 好的,我告诉你,为什么我不能为此创建一个表。 我正在使用IOS apns服务器进行推送通知function。 当我配置它时,它在没有模型的情况下在我的数据库中创建了很多表。 在其中一个表中,我想在单个查询中批量插入记录。 最初我用循环做了。 它影响了性能。 所以,我想进行优化。 无论解决方案是什么,请建议我。 以下是我的方法。 # Push Notification to all users of the application. def ios_push_notification(admin_notif) bulk_data = [] n = Rpush::Apns::Notification.new n.app = Rpush::Apns::App.find_by_name(“ios_app”) ios_user_reg_ids = UserRegId.where(:device_os=>”ios”).pluck(:user_gcm_reg_id) ios_user_reg_ids.each do |device_token| n.device_token = device_token n.alert = admin_notif.try(:content) n.data = { foo: :bar } bulk_data […]

finder_sql不使用Rails解析字符串

我有一个问题,我用于finder_sql的查询在被移交给PostgreSQL之前没有被正确解析导致数据库语法错误。 为了说明这个问题我刚才使用了示例代码: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html 我只将class_name更改为”User”因为我没有人模型,但这并不重要。 has_many :subscribers, :class_name => “User”, :finder_sql => ‘SELECT DISTINCT people.* ‘ + ‘FROM people p, post_subscriptions ps ‘ + ‘WHERE ps.post_id = #{id} AND ps.person_id = p.id ‘ + ‘ORDER BY p.first_name’ 当我使用它时,我收到以下错误: User Load (0.3ms) SELECT DISTINCT people.* FROM people p, post_subscriptions ps WHERE ps.post_id = #{id} AND ps.person_id = […]

由find_or_create_by_创建的重复记录

我有一个ActiveRecord对象,公司,我的项目中唯一一个调用此对象的实例的调用如下所示: corp = Corporation.find_or_create_by_eveid_and_user_id(self.corporation_eveid, self.account.user_id) 然而,不知何故,在我的应用程序快乐地运行了几天后,有重复的记录 – 记录eveid和user_id具有相同的值。 这怎么可能? 在我更新这些会导致此问题的记录的方式中,我可能做错了吗? 我最终为表添加了一个唯一的复合索引。 这应该可以解决问题,但我不明白它是如何发生的。 这是Rails 3.0.7。

按共同关联数排序(Rails)

背景:我有post和用户,都有很多社区。 目标:对于任何给定的用户,我想返回一组post,按照post与用户共有的社区数量排序(更多社区的post更高) 我当前的尝试(使用排序方法)有效: Post.includes(:community_posts).where(community_posts: { community_id: current_user.community_ids }).sort{ |x,y| (y.community_ids & current_user.community_ids).length (x.community_ids & current_user.community_ids).length } 但有没有更好/更有效的方法来做到这一点?

Rails属于通过关联

我正在尝试将新模型添加到现有模型网格中。 现有的工作完美,但我不能让新工作正常工作,我想知道协会是否能够以我试图使其工作的方式工作。 更新:我刚刚被问到: belongs_to through是我在阅读这个问题时读过的东西。 如果它不存在,那么has_one through是否正确? 我也试过了,但也没用。 这是现有的网格: class Course has_many :users, through: :enrollments has_many :enrollments end class User has_many :courses, through: :enrollments has_many :enrollments end class Enrollment belongs_to :course belongs_to :user # has fields :user_id, :course_id end 现在,用户应该能够对他完成的课程进行评分。 (如果有的话,有一个他的身份证和课程ID的注册。)我认为最好写如下: class Course has_many :users, through: :enrollments has_many :enrollments has_many :ratings, through: :enrollments end class […]

ActiveRecord连接:转换为字符串的整数列

我有一个名为Article的模型,我正在加入,如下所示: articles = Article.joins(“LEFT OUTER JOIN twitter_shares ON articles.id = twitter_shares.article_id”).where(“articles.id = ? or articles.id = ?”, 27165, 5632).select(“articles.id, twitter_shares.user_id”) 当我收回文章并检查返回的Article模型时,它会将user_id作为字符串返回,即使twitter_shares表中的列类型是整数。 为什么是这样? 如何让ActiveRecord将整数列作为整数而不是字符串返回?

跳过相关对象的validation – rails activerecord

class Author has_many :books validates :email, :presence => true end class Book belongs_to :author validates :title, :presence => true end 跳过validation很简单: a = Author.new a.save(:validate => false) 但是,在创建书籍而不跳过书籍validation时,我需要跳过作者validation,如下所示: b = Book.new b.title = “A Book” b.author = Author.last b.save

Rails – 如何更新控制器中的单个属性

我是rails的新手并试图完成一项简单的任务。 我想在图像点击上切换布尔属性“完成”。 在我看来,我的链接看起来像: :calendars, :action=>:toggle_done,:id=> feed_item.id, :title => “Mark as done”, :remote=> true, :class=>”delete-icon” %> 我添加了一条路线如下: resources :calendars do get ‘toggle_done’, :on => :member end 在控制器中,我创建了一个方法: def toggle_done @calendar = Calendar.find(params[:id]) toggle = !@calendar.done @calendar.update_attributes(:done => toggle) respond_to do |format| flash[:success] = “Calendar updated” format.html { redirect_to root_path } format.js end 当我点击图像时,没有任何反应我看到以下错误: Started GET “/toggle_done” […]

如何在新的has_many关联上使ActiveRecord bump updated_at

将记录添加到has_many关联并保存父级时,该父级的updated_at不会被碰撞: order = Order.new order.save pp order => # order.line_items < [#] order.save pp order.reload => # 这是有道理的,因为Order它没有触及; 外键存在于LineItem 。 但是,在这种情况下,我想查询Order并找到在过去30分钟内收到新LineItem的Order (即:在过去30分钟内“更新”)。 我不确定这里最好做什么,以及如何轻松实现这一目标。 是否有一些标志或方法我可以设置让AR更新父母updated_at如描述? 或者我应该通过一些钩子来做到这一点? 如果是这样,什么挂钩? 或者我应该将此时间戳存储在不同的列中? 请注意,我可以通过join(:line_items)查询line_items.updated_at ,但这可能会使我的代码更混乱,性能更差:或者这是Rails中此类问题的首选方式?

如何创建与ActiveResource对象的ActiveRecord关系?

假设我正在为已经拥有People应用程序的出版公司编写一个Library应用程序。 所以在我的图书馆应用程序中我有 class Person < ActiveResource::Base self.site = "http://api.people.mypublisher.com/" end 现在我想为每个Person存储Article : class Article :author end 我想我的数据库中有以下表格: Articles id (PK) | title (string) | body (text) | author_id (integer) author_id不完全是外键,因为我没有People表。 这留下了几个问题: 如何告诉我的Person ActiveResource对象它has_many Articles ? Articles.find(:first).author工作吗? 鉴于没有ActiveRecord和没有支持表,will belongs_to会工作?