Tag: activerecord

如何在Sinatra / Active Record中限制数据库字符串值的字符/字数?

我的ActiveRecord数据库中有一列我希望有一个字数限制。 基本上,我创建了一个允许用户输入文本(字符串)的表单。 我想限制该字符串中允许的字符数。 @allposts = Post.limit(20) 这是我目前在发布所有内容的/current页面的get方法中所拥有的。 20 =显示的post数量。 我还有一个/new页面,用户将发布新内容。

Activeresource,更新和合并

我遇到了一个问题,我现在无法处理一个看起来像这样的ActiveResource对象: #1, “created_at”=>”2012-01-10T14:54:36Z”, “id”=>1, “settings_hash”=> # # [“none”, “none”]}, @persisted=false, @prefix_options={}>, “permissions”=> # [[“all”], [“all”] ]}, @persisted=false, @prefix_options={}>}, @persisted=false, @prefix_options={}>, “field_key”=>”BRp7Box33sg”, “fieldone”=>1, “updated_at”=>”2012-01-10T14:54:36Z”}, @persisted=true, @prefix_options={}> 我们来自ActiveRecord模型,其中字段被序列化为哈希。 我觉得好像我正在做很多工作来解压缩哈希并且我似乎无法添加它并更新模型。 我的意思是我可以添加它同时玩弄它但它需要类似的东西: thisobject.settings_hash[‘permissions’].attributes.merge({“derp”=>[[‘dsfasdf’],[‘asdfasdfadsf’]]}) 我很难回到对象,然后我想更新它来自哪里。 所以,我正在做一些我现在还不太了解的事情,也许是以一种尴尬的方式。 建议? 我想在ActiveResource对象中添加哈希到哈希字段以在其他地方更新ActiveRecord对象实例,但目前还不能。

ActiveResource +缓存

我正在构建一个应用程序,它通过带有ActiveResource的api来使用模型。 我注意到了 @resource ||= @resource.do a query 不起作用,即如果我在我的控制器中放了类似的东西,我的应用程序仍然会查询api。 因此,我没有使用ActiveRecord的内置缓存。 是时候扩大我的知识和技能基础了。 我找到了这个: http : http : //injectisforwizards.com/blog/read-through-caching-of-activeresource/ ,虽然我还不理解这个100%,但是对于那些执行.find的基于控制器的查询,这似乎有效。 但不是我有任何自定义查询,例如: @current_resource ||= Resource.get(:resource_all, :by_account=>@current_account.account_key) (命中自定义控制器并运行范围,返回集合) 我正在研究这个问题,我会发现发生了什么,但我很好奇是否有人可以告诉我更简单的术语是什么,我可以做些什么来平滑ActiveResource中的缓存更像ActiveRecord,如何我可以定制这个以缓存所有查询等。任何事情都会有所帮助。 编辑: 我发现了这个: https : //github.com/Ahsizara/cached_resource看起来很有前景,但它是新的(并在上面建立了链接)….值得注意的是它似乎没有处理任何类型的集合,但是一个资源很好地查找/缓存。

Rails应用程序错误 – ActiveRecord :: PendingMigrationError正在等待迁移; 运行’rake db:migrate RAILS_ENV = development’来解决此问题

创建数据库,创建表,数据存在。 但是在我重新启动Rails应用程序之后,我收到了这个错误。 该应用程序正在使用MySQL。 这有什么问题? 先感谢您

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

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

如何使用嵌套属性在多对一自引用模型中创建/更新Rails中的对象?

我的Rails 4应用程序有一个主题模型,允许多对多的自引用关系。 我基于这个奇妙的答案设置了模型,为多对多自连接创建模型,如果我手动填充关系表,它似乎有效。 让我们使用该模型来解决问题: user.rb: class User < ActiveRecord::Base # follower_follows "names" the Follow join table for accessing through the follower association has_many :follower_follows, foreign_key: :followee_id, class_name: "Follow" # source: :follower matches with the belong_to :follower identification in the Follow model has_many :followers, through: :follower_follows, source: :follower # followee_follows "names" the Follow join table for […]

搜索表单没有路由到Rails 5.1中的适当控制器

我在这一天上已经挣扎了几天: 我的导航栏中有一个搜索表单,通过作业模型在我的Users表的:address列中运行搜索。 这是我的JobsController的索引方法: def index @jobs = if params[:address] Job.joins(:user).where(users: { address: params[:address].downcase }).paginate(page: params[:page], per_page: 3) else @jobs = Job.paginate(page: params[:page], per_page: 3).order(‘id DESC’) end end 这是表格: 这是我的路线档案: Rails.application.routes.draw do devise_for :users get ‘static_pages/home’ get ‘static_pages/faq’ get ‘static_pages/about’ get ‘static_pages/cgu’ root ‘static_pages#home’ get ‘/faq’, to: ‘static_pages#faq’ get ‘/about’, to: ‘static_pages#about’ get ‘/contact’, to: ‘static_pages#contact’ […]

为什么ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:near“)”:语法错误:INSERT INTO“user_friendships”()VALUES()

为什么ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:near“)”:语法错误:INSERT INTO“user_friendships”()VALUES() 试图测试时: $ ruby​​ -I test test / unit / user_friendships_test.rb require ‘test_helper’ class UserFriendshipsTest < ActiveSupport::TestCase should belong_to (:user) should belong_to (:friend) test "that creating a frinedship works without raising an exception" do assert_nothing_raised do UserFriendship.create user: users(:a), friend: friends(:b) end end end 任何的想法? 更新:他是Schema.rb的一部分 create_table “user_friendships”, :force => […]

在ruby脚本上使用活动记录和mysql

我一直在尝试在ruby脚本上使用mysql和活动记录,但我收到以下消息:过时的mysql gem。 升级到2.8.1或更高版本。 在你的Gemfile中:gem’mysql’,’2.8.1’。 或者使用gem’mysql2’(RuntimeError)。 在我收到关于gem’mysql2’缺失的消息之前,然后我检查了活动记录中的连接适配器上的源代码,并看到它需要我没有安装的mysql gem。 这是我的脚本: require ‘rubygems’ require ‘active_record’ require ‘mechanize’ require ‘nokogiri’ require ‘active_record’ require ‘mysql2’ gem ‘mysql2’ ActiveRecord::Base.establish_connection ({ :adapter => “mysql”, :host => “localhost”, :username => “root”, :password => “”, :database => “rainalytics”}) ActiveRecord::Schema.define(:version => 20110320035328) do create_table “score_logs”, :force => true do |t| t.integer “blog_posts” t.integer “featured” end […]

Rails不会找到CSV固定装置

我正在尝试使用CSV灯具,但是当我做rake db:fixtures:load ,它抱怨test/fixtures缺少YAML文件。 当灯具在YAML中并且文件以.yml结尾时, .yml 。 此外,当我查看activerecord-3.2.3/lib/active_record/fixtures.rb中的代码时,我完全看不到对CSV处理的引用。 看来fixtures.rb只知道如何使用YAML灯具。 这是什么? CSV固定装置已被弃用?