Tag: 数据库

Ruby on Rails:表关系搜索

我仍然无法搜索存储在单独表中的技术,其中技术表(technol)和Project表之间通过名为projecttechnol的表之间存在关系。 当我尝试使用技术(tech1)搜索项目时,这是我的日志。 Parameters: {“utf8″=>”✓”, “client”=>””, “industry”=>””, “role”=>””, “technols”=>{“id”=>[“”, “1”, “”]}, “business_div”=>””, “project_owner”=>””, “start_date_dd”=>””, “start_date_A”=>””, “start_date_B”=>””, “status”=>””, “keywords”=>””, “per_page”=>”10”} User Load (0.6ms) SELECT “users”.* FROM “users” WHERE “users”.”id” = 1 LIMIT 1 Technol Load (0.3ms) SELECT “technols”.* FROM “technols” Project Load (0.5ms) SELECT “projects”.* FROM “projects” ORDER BY client Project Load (0.6ms) SELECT “projects”.* FROM “projects” […]

如何计算用户对我的特定项目(Rails)所做的所有喜欢

我为新手问题道歉,我对Rails和SQL相对较新。 我正在尝试计算用户对我的特定项目所做的所有喜欢。 在SO社区和Treehouse Coding的帮助下,我能够显示哪些用户喜欢我的特定项目。 此外,我能够研究如何在我的控制器中使用.join显示我收到的所有喜欢(来自Lisa,James,Harry等)。 现在我想显示每个特定用户的计数。 所以,例如,如果詹姆斯喜欢我的4件物品,我想在詹姆斯旁边展示4件。 我在下面列出了所有相关代码,非常感谢你们! Index.html.erb My Fans – Items_controller def index @items = Item.order(“created_at DESC”) if current_user.present? @likers = current_user.items.map(&:likes).flatten.map(&:user).flatten @likersnumero = current_user.items.joins(:likes).map(&:user).count end end item.rb的 class Item true end users.rb的 class User < ApplicationRecord has_many :likes def likes?(post) post.likes.where(user_id: id).any? end #Tried using def total_likes in my index.html.erb using but […]

最后在Windows 64位中安装了MySQL for Rails,现在

环境: Windows 7 64bit (Home Premium) Ruby 1.9.2 p290 (2011-07-09) [i386-mingw32] Rails 3.0.10 MySQL 5.5 Ruby,尽管我已经成功安装了mysql2 rubygem,但是当我调用需要它的rails命令时,我认为缺少gem。 在晚上获得在Windows 64位上安装mysql ruby​​gem之后,我决定(最终)开始将mysql链接到本地​​rails服务器。 这就是我试图这样做的方式…… (相当)来自cmd.exe精确抄本 C:\rails\cookbook>gem install mysql –platform=ruby — –with-mysql-include=c:\mysql\include –with-mysql-lib=c:\mysql\lib Temporarily enhancing PATH to include DevKit… Building native extensions. This could take a while… Successfully installed mysql-2.8.1 1 gem installed Installing ri documentation for mysql-2.8.1… Installing […]

ActionController的:: ParameterMissing

我创建了一个具有personsController类的应用程序,具有动作节目和新节目。 我也用这种方式创建了Person模型: rails generate model Person name:string surname:string partner:references partner字段应该引用另一个人。 在新操作中,我创建了一个表单以将新人插入数据库: 这是personsController方法创建的: def create @person= Person.new(post_params) @Person.save redirect_to @person private def post_params params.require(:post).permit(:partner,:name,:surname); end 问题是我提交表单时出错: 更新 我把指令改为: params.require(:person).permit(:partner,:name,:surname); 但我仍然得到一个错误: PersonsController中的NameError #create 未初始化的常数Person :: Partner 人物模型: class Person < ActiveRecord::Base belongs_to :partner end

保存到数据库的不同日期 – 错误的时区

当我使用Javascript从FullCalendar获取日期时,我有以下值: Fri Sep 13 2013 08:30:33 GMT-0400 (EDT) 将该日期保存到数据库后,我发现在数据库中有不同的值: 2013-09-13 12:00:00.000000 因此,date会自动转换为UTC并像这样保存到数据库中。 如何将正确的日期保存到数据库中? 我不想硬编码:) 谢谢。

Rails – 当我推向生产时,我会丢失我的开发数据库吗?

我知道这可能是一个愚蠢的问题,但…… 简而言之,我有一个应用程序,其中包含数百个处于开发模式的记录的数据库。 当我推向生产并部署到互联网上时,我是否会丢失数据库并且必须在生产模式下重做所有内容? 只是安全!

Ruby on Rails:NilClass的未定义方法`model_name’:Class

我试图允许用户将项目输入数据库。 其中一个字段允许他们为该项目输入多种技术。 这是我的项目控制器,新建和创建动作。 def new @project = Project.new @all_technols = Technol.all @project_technol = @project.projecttechnols.build respond_to do |format| format.html # new.html.erb format.json { render json: @project } end end def create @project = Project.new(params[:project]) params[:technols][:id].each do |technol| if !technol.empty? @project.projecttechnols.build(:technol_id => technol) end end end 这是我的多选技术下拉列表的新项目视图。 true} ) %> 目前,我有一个用户可以输入新技术的页面。 但我想将此选项移至创建新项目页面,在该页面中,他们可以选择现有技术,也可以输入新技术,或同时执行这两项,并且可以使用该项目进行保存。 编辑:更改问题,以及添加模型文件 当我尝试保存新项目时,我收到此错误。 undefined method `model_name’ […]

使用mongoid / mongodb提取,建模和更改数据模型

我正在寻找一种更好的方法,如果可能的话,使用惊人的mongoid ODM驱动程序来操纵现有的mondodb数据模型。 假设您有一个嵌入的一对多数据模型,如下所示: class User include Mongoid::Document field :nickname embeds_many :watchlists end class Watchlist include Mongoid::Document field :html_url field :description field :tags_array, type: Array embedded_in :user end 现在,对于所有用户,您只想提取每个监视列表的一部分,当且仅当它具有 tags_array == [“ruby”, “web”, “framework”] 只返回几个字段(不是整个监视列表文档): watchlist的html_url内容 关注列表的描述内容 和 相关父昵称(User.nickname) 我试过这样的事情: 1.9.2p290 :574 > Competitor = Struct.new(:html_url, :description, :user) => # 1.9.2p290 :575 > competitors = [] […]

无法在视图Rails 4中显示我的类别和子类别

我的网站有分类和子类别,这里是我在我的种子.rb,我创建了一个名为“video和动画”的主要类别,和4个子类别,比我将子类别分配到主类别。 @category = Category.create!(name: “Video and animation”) [“Intro”, “Animation & 3D”, “Editing and Post Production”, “Other”].each do |name| @subcategory = Subcategory.create!(name: name, category_id: @category.id) end 它运行良好,在我的rails控制台中,我看到一切正常,添加产品的category_id更改为数字(整数应该如此)。 问题:如何在我的视图中显示该类别,因此当有人点击它时,他会获得该类别的所有产品。 我如何按照相同的原则显示所有子类别。 以下是我试图提出意见的内容 奇怪的是,当我把我的产品控制器放进去时 def index @category_id = Category.find_by(name: params[:category]) @gigs = Gig.where(category_id: @category_id).order(“created_at DESC”) @subcategory_id = Subcategory.find_by(name: params[:subcategory]) @gigs = Gig.where(subcategory_id: @subcategory_id).order(“created_at DESC”) end 它显示了我想要的所需子类别,但主要类别仍为空。 如果我把它放入我的控制器 def index […]

Rails连接到database.yml指定的数据库

我最近重启了战俘。 在重新加载我正在处理的Rails应用程序时,它挂了。 检查Rails的日志我每隔5-20秒重复一次: Connecting to database specified by database.yml 我可以通过Rails控制台毫无问题地与Postgres数据库进行交互,因此没有问题。 我在我的database.yml中没有改变任何东西几周,所以我认为问题不在那里。 可能是什么问题,我该如何调试它?