Tag: 数据库

通过对另一个表中的值进行分组来对订单表进

我有一个问题表:标题每个问题都有答案,在另一个表中:text,question_id每个答案都有投票,在另一个表中:answer_id 我可以要求总票数 question.votes.count 我已经看到如何在http://guides.rubyonrails.org/active_record_querying.html#group上对投票数据库进行分组 Vote.group(“answer_id”) 但我想通过投票来命令我的问题。 这是通过另一个表的分组从表中排序数组。 那可能吗? q=Question.last q.answers.order_by_vote #How to do this? 我认为我的答案可能是在答案模型上做一个范围,该范围使得对属于该问题的投票子集进行分组。 我对吗? 谢谢。

铁路服务器错误? (轨道3)在窗户

我使用命令创建了一个新项目: rails new simple_cms -d mysql 但是当我尝试启动Web服务器时,会发生这种情况: C:\Users\johnny\Sites\simple_cms>rails server C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require’: 193: %1 is not a valid Win32 application. – C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/1.9/mysql2.so (LoadError) from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2.rb:9:in `require’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2.rb:9:in `’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.1/lib/bundler/runtime.rb:68:in `require’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.1/lib/bundler/runtime.rb:68:in `block (2 levels) in require’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.1/lib/bundler/runtime.rb:66:in `each’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.1/lib/bundler/runtime.rb:66:in `block in require’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.1/lib/bundler/runtime.rb:55:in `each’ from C:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.1.1/lib/bundler/runtime.rb:55:in `require’ […]

Rails 4 – 在没有数据库的情况下validation模型

我已经按照本教程并尽可能地为Rails 4制作它。 http://railscasts.com/episodes/219-active-model?language=en&view=asciicast class Contact include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming validates :name, :email, :phone, :comment, :presence => true def initialize(attributes = {}) attributes.each do |name, value| send(“#{name}=”, value) end end def persisted? false end private # Using a private method to encapsulate the permissible parameters is just a good pattern # since you’ll be […]

如果数据库为空,则处理ActiveRecord错误

我正在使用rails 4 app,我有以下控制器代码 def index @issue = Issue.find(1) @sections = @issue.sections @articles = @issue.articles end 如果数据库为空并且出现错误,则会中断:“无法找到id = 1的问题”。 检查这个问题的正确方法是什么,如果数据库中没有任何内容,它不会引发错误?

两个表作为Rails中的一个模型

是否可以在rails中设置依赖于两个表的连接的模型? 这意味着,为了找到/更新/销毁模型记录,需要在连接中链接在一起的两个数据库表中的两个记录。 该模型将只是两个表的所有列包装在一起,然后可以用于表单等。 这样,当模型被创建/更新时,它只是一个应用于模型的表单变量哈希? 在Rails 2或3中这可能吗?

一次只允许一个用户编辑内容

例如,如果您想编辑client-site.com/pages/5 ,请转到admin-site.com/pages/5/edit 。 我想让一个管理员一次访问admin-site.com/pages/5/edit 。 我计划做的是在数据库中创建“editor_id”和“editing_rights_expiration_time”列,并使用JavaScript /服务器端逻辑更新它们。 有没有更好的方法来实现此function? 我正在使用Rails 3。

Paperclip安装后未定义的方法has_attached_file?

不确定我是否已经在这里做了一切。 我将Paperclip gem添加到我的Gemfile中并进行了bundle install。 我按照Paperclips Github页面上的自述文件说明进行操作。 我不确定是否需要安装ImageMagick。 但是,我在Github上发现了一个在Snow Leopard上安装的脚本,所以我跑了: rails plugin安装http://github.com/masterkain/ImageMagick-sl.git 但是,现在我得到了Undefined方法has_attached_file 我是Rails和编程的新手。 对我来说大部分都是非常先进的,但到目前为止,我已经设法解决了问题。

Postgresql“没有pg_hba.conf条目”错误

我正在为一个rails项目(我正在加入)设置我的本地postgresql数据库,并完成了以下操作: 安装postgres 添加/捆绑’pg’gem 创建名为“foobar”的db用户 创建了一个名为“foobar_development”的数据库 rails应用程序附带了rake db:migrate任务。 当我运行此任务时,我得到以下错误输出: FATAL: no pg_hba.conf entry for host “::1”, user “foobar”, database “foobar_development”, SSL off 我在以下位置找到了一个pg_hba.conf文件: 在/ usr /本地的/ var / Postgres的 这是pg_hba.conf文件的相关部分: # TYPE DATABASE USER ADDRESS METHOD # “local” is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all […]

SQLite3 :: ConstraintException:约束失败:

我正在尝试学习Ruby on Rails并遇到了我需要帮助的问题。 我想要做的是创建一个带有表单的“项目经理”,但是当我填写表单并点击提交时,会显示以下错误: ActiveRecord::StatementInvalid in ProjectsController#create SQLite3::ConstraintException: constraint failed: INSERT INTO “projects” (“created_at”, “description”, “end_date”, “start_date”, “title”, “updated_at”, “user_id”) VALUES (?, ?, ?, ?, ?, ?, ?) Rails.root: /Applications/XAMPP/xamppfiles/htdocs/IDV450-labb1 Application Trace | Framework Trace | Full Trace app/controllers/projects_controller.rb:14:in `create’ 项目管理员: 1. class ProjectsController current_user.id)) 13. 14. if @project.save 15. redirect_to projects_path 16. else 17. […]

Rails db:seed error“undefined method`finder_needs_type_condition?’ 为零:NilClass“

尝试填充我的sqlite数据库时遇到问题。 关于特定错误“finder_needs_type_condition?”的信息不多。 我可以找到,但我对Rails没有多少经验,甚至怀疑问题可能在哪里。 模型: class Character < ActiveRecord::Base validates :user_id, :name, :class, presence: true end 控制器: class CharactersController < ApplicationController before_action :authenticate_user! respond_to :json @user_id = current_user[:id] def index @characters = Character.all end def show @character = Character.find(params[:id]) end def new @character = Character.new end def create @characters = Character.all @character = Character.create(character_params) end private […]