activerecord

ActiveRecord… activerecord-mysql-adapter

点点圈 提交于 2019-12-21 07:39:22
问题 This is driving me farking insane. I'm trying to create a simple beginner app utilizing ActiveRecord to do simple database tasks. I'm NOT using rails. I keep getting the error: RuntimeError: Please install the mysql adapter: `gem install activerecord-mysql-adapter` (Could not find mysql (~> 2.8.1) amongst [actionmailer-3.1.3, actionpack-3.1.3, activemodel-3.1.3, activerecord-3.1.3, activerecord-sqlserver-adapter-3.1.4, activeresource-3.1.3, activesupport-3.1.3, ansi-1.4.1, arel-2.2.1,

ActiveRecord… activerecord-mysql-adapter

随声附和 提交于 2019-12-21 07:39:11
问题 This is driving me farking insane. I'm trying to create a simple beginner app utilizing ActiveRecord to do simple database tasks. I'm NOT using rails. I keep getting the error: RuntimeError: Please install the mysql adapter: `gem install activerecord-mysql-adapter` (Could not find mysql (~> 2.8.1) amongst [actionmailer-3.1.3, actionpack-3.1.3, activemodel-3.1.3, activerecord-3.1.3, activerecord-sqlserver-adapter-3.1.4, activeresource-3.1.3, activesupport-3.1.3, ansi-1.4.1, arel-2.2.1,

Mocking ActiveRecord relationship beheavior in RSpec tests

十年热恋 提交于 2019-12-21 07:28:39
问题 I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts. I'm trying to spec out a code block that includes something like this: user = User.find(123) post = user.posts.find(456) I know how to mock out the User.find and user.posts parts. The user.posts mock returns an array of Post objects. And when it get's to .find(456) part, everything breaks down with no block given exception. So my question here is: what do I return as the result

unknown attribute: user_id

≡放荡痞女 提交于 2019-12-21 07:22:04
问题 I'm getting the error unknown attribute: user_id durring execution of current_user.stories.build class User < ActiveRecord::Base has_many :stories, class_name: 'Story', foreign_key: 'user_id', dependent: :destroy ... class Story < ActiveRecord::Base belongs_to :user, class_name: 'User', foreign_key: 'user_id' ... schema.rb create_table "stories", :force => true do |t| t.string "responsible" t.string "descr" t.string "state" t.datetime "created_at", :null => false t.datetime "updated_at",

unknown attribute: user_id

假如想象 提交于 2019-12-21 07:21:49
问题 I'm getting the error unknown attribute: user_id durring execution of current_user.stories.build class User < ActiveRecord::Base has_many :stories, class_name: 'Story', foreign_key: 'user_id', dependent: :destroy ... class Story < ActiveRecord::Base belongs_to :user, class_name: 'User', foreign_key: 'user_id' ... schema.rb create_table "stories", :force => true do |t| t.string "responsible" t.string "descr" t.string "state" t.datetime "created_at", :null => false t.datetime "updated_at",

Pluck associated model's attribute in Rails query

蓝咒 提交于 2019-12-21 07:13:35
问题 In my rails app, collections have many projects , and projects have many steps . I'd like to grab all the ids of steps in a collection's projects, and I'm wondering if I can do it all in one query. For example, I know I can do the following step_ids = [] @collection.projects.each do |project| project.steps.each do |step| step_ids << step.id end end But is it possible to do something like the following: @collection.projects.include(:steps).pluck("step.id") // syntax here is not correct 回答1:

Ruby on Rails Active Record return value when create fails?

淺唱寂寞╮ 提交于 2019-12-21 07:12:39
问题 I am new to ruby on rails and having trouble getting this work. Basically I have a user registration page which has a password confirmation. In the User class I have the following validation: validates :password, confirmation: true And in the controller I have def create vals = params[:user] if(User.exists(vals[:username])) flash[:warning] = "#{vals[:username]} already exists! Please try a new one. " else vals[:create_date] = DateTime.current user = User.create(vals, :without_protection =>

Rails: How to use before_save to change a field value based on another field?

匆匆过客 提交于 2019-12-21 07:09:23
问题 I'm trying to set a boolean field to false based on the value of another boolean field. I tried the following with an ActiveRecord model: before_save :reconcile_xvent def reconcile_xvent self.xvent_hood = false if !self.xvent_plenum? end But this doesn't work. Now, many of my unit tests fail with: ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved How can I set xvent_hood to be false if xvent_plenum is false? Update Here's what works (some of which comes from the comments/answers

rails 3 & activerecord: do I need to take special record locking precautions to update a counter field?

若如初见. 提交于 2019-12-21 06:56:07
问题 Each time a user's profile gets displayed we update an integer counter in the Users table. I started thinking about high-concurrencey situations and wondered what happens if a bunch of people hit a user's profile page at the exact same time: does rails/activerecord magically handle the record locking and semaphore stuff for me? Or does my app need to explicitly call some sort of mechanism to avoid missing update events when concurrent calls are made to my update method? def profile_was_hit

rails 3 : Do i need to give return true in a before_save callback for an object.save to work?

巧了我就是萌 提交于 2019-12-21 06:55:28
问题 Class User before_save :set_searchable def set_searchable self.searchable = true if self.status == :active end end >> u = User.last >> u.save false u.save always return false. If i remove the before_save it works also if i give a return true in before_save it works so do i need to give return statements in before_save ? will ActiveRecord saves an object if the before_save returns false ? Where can i see a full documentation regarding callbacks and its workflow . Thanks in advance 回答1: From: