activerecord

Rails ActiveRecord 3.2: How to skip before_delete callback in child model?

强颜欢笑 提交于 2019-12-11 01:39:15
问题 I'm working in Rails 3.2.16. In my app, an account has_many users. An account must always have admin users, so you can't destroy them. This takes care of that: class Account < ActiveRecord::Base has_many :users, :dependent => :destroy end class User < ActiveRecord::Base before_destroy :check_if_admin def check_if_admin false if self.is_admin end end However, when you destroy the whole account, the admins should be destroyed as well. Instead, when I call @account.destroy from the controller,

Rails Find when some params will be blank

萝らか妹 提交于 2019-12-11 01:38:21
问题 I have a search form with two fields: x, y When the search is performed it will look for records that match all conditions. However any of the two conditions can be set to 'All' by leaving it blank. What should I set params[:x] and params[:y] to if they are set to all. params[:x] = ? unless params[:x] params[:y] = ? unless params[:y] users = User.where(["x = ? AND y = ?", params[:x], params[:y]]) 回答1: I would suggest building up your conditions using a hash: conditions = {} conditions[:x] =

How do you set an attribute when creating an ActiveRecord object?

别等时光非礼了梦想. 提交于 2019-12-11 01:36:43
问题 I am trying to set an attribute on an object that I am creating. I feel like this should work: def create @album = Album.new(params[:album]) @album.user = current_user if @album.save flash[:notice] = 'Album was successfully created for ' + current_user.login + '.' redirect_to albums_url else render :action => "new" end end But it seems to ignore the assignment to the user field. Any ideas? 回答1: Assuming that your model relationships are set up correctly*, it's better to do: @album = current

Scoping an association search with Ransack

会有一股神秘感。 提交于 2019-12-11 01:22:24
问题 I have a User model with an Enrollment model associated with it. A User can have many Enrollments but each Enrollment only has one Company . My Enrollment model has a flag_contractor boolean. So if I have a User search such as :enrollment_flag_contractor false I can get inconsistant results as I am trying to scope my enrolments for the current company (via the Apartment gem). For example I have a user that has an enrollment in two separate companies, one with flag_contractor = true and one

Sinatra - ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base

六眼飞鱼酱① 提交于 2019-12-11 01:20:43
问题 I've only been able to find rails answers to this question. Whenever I run rake db:migrate I am getting the aforementioned error. As far as I am aware, I have setup everything correctly so have no idea what's wrong. config/environment.rb ENV['SINATRA_ENV'] ||= "development" require 'bundler/setup' Bundler.require(:default, ENV['SINATRA_ENV']) configure :develpoment do set :database, 'sqlite3:db/database.db' end require './app' Rakefile require "./config/environment" require "sinatra

Pattern for translating ActiveRecord validation errors to API responses

廉价感情. 提交于 2019-12-11 01:19:15
问题 I have a Rails 2.x app that I'm building a RESTful interface for. For some (good) reason, I've chosen to expose some of the fields on my ActiveRecord models through the API using different names than the underlying fields in MySQL. For example, a lot of my MySQL field were prefixed with the model name (user_type, user_first_name, etc.). My request/responses use the names without prefixes (type, first_name). The action methods look like (and yes, this is already a source of pain for

Accessing additional values on has_many through Rails

China☆狼群 提交于 2019-12-11 01:18:43
问题 I am having real trouble accessing the value of an additional parameter called permission on a has_many through. It is probably something simple. My 3 Models are class User < ActiveRecord::Base has_many :players_users has_many :players, through: :players_users end class Player < ActiveRecord::Base has_many :players_users has_many :users, through: :players_users end class PlayersUser < ActiveRecord::Base belongs_to :user belongs_to :player validates :player_id, uniqueness: { scope: :user_id }

ActiveRecord nested SELECT

断了今生、忘了曾经 提交于 2019-12-11 01:07:27
问题 I need help with SELECT FROM nested SELECT. How can I rewrite following query in an ActiveRecord way and get an Relation object? SELECT candidates.* FROM (SELECT (candidates.first_name || ' ' || candidates.last_name) AS full_name, candidates.* FROM candidates) candidates WHERE full_name = 'Anton Kolganov' 回答1: Why do you concatenate instead of selecting based on first and last name? The subselect will be much less performant than direct query. You could get the full name using the select

How to avoid ActiveRecord model double saving?

狂风中的少年 提交于 2019-12-11 01:05:39
问题 Model "One" class One < ActiveRecord::Base before_save :do_stuff private def do_stuff two = Two.find(8) two.field2 = 'Value' two.save! end end Model "Two" class Two < ActiveRecord::Base before_save :do_stuff private def do_stuff one = One.find(7) one.field2 = 'SomeValue' one.save! end end Executing: two = Two.find(1) two.somefield = 'NewVal' two.save! Infinite loop will start. What would be most ruby-on-rails way to implement two models that must change each other on before_save callback? 回答1

HABTM associations in Rails : collecting and counting the categories of a model's children

╄→гoц情女王★ 提交于 2019-12-11 01:01:50
问题 I have a has_and_belongs_to_many relationship setup. It looks like this: books have_and_belong_to_many categories categories have_and_belongs_to_many books a store has_many books a book belongs_to a store I'm trying to show how many books in each store belong to each category. So my view would show Store X has 200 books and 80 of them are mystery, 60 are non fiction, etc. I have been trying out a bunch of different ways of doing this, but no success so far. I think I'm starting in the wrong