activerecord

Rails app error - ActiveRecord::PendingMigrationError Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-05 13:35:46
问题 The database is created, tables are created, data are there. But after I restarted the Rails application, I got this error. The app is using MySQL. What's the issue here? Thank you in advance 回答1: Solution Simply run rake db:migrate There are migrations that need to be ran before your server can start. Learn what migrations are and how they are used in Rails with this RailsGuide. Run that rake db:migrate command any time you make a migration, or any time you create a new project. If you get

How to validate a single attribute for a submitting ActiveRecord instead of all its attributes?

本小妞迷上赌 提交于 2020-02-05 04:33:15
问题 I am using Ruby on Rails 3 and I would like to validate a single attribute for a submitting ActiveRecord instead of all its attributes. For example, in my model I have: validates :firstname, :presence => true, ... validates :lastname, :presence => true, ... I would like to run validation on the :firstname and on the :lastname separately . Is it possible? If so, how can I make that? P.S.: I know that for validation purposes there are methods like "validates_presence_of", "validates

Incorporating Custom SELECT clause in ActiveRecord query

早过忘川 提交于 2020-02-03 02:05:48
问题 I have a large table of assessments, in that table there are several integer columns with numerical scores. I am trying to remove some N+1 iteration to speed up my application. I can generate this query in raw SQL to gather all of the scores query = 'SELECT ' + Assessment.rating_attributes.collect{|attribute| 1.upto(5).collect do |i| %Q{SUM(CASE WHEN #{attribute.to_s} = #{i} then 1 else 0 end) as #{attribute}_score_#{i}} end }.join(', ') + ' FROM assessments' When I execute this query with: $

problems showing only projects that belong to a user

♀尐吖头ヾ 提交于 2020-02-02 13:33:12
问题 So I am building a rails app where you can display projects and so on and so forth. I have the following code in my projects controller: def create @project = Project.create(params[:project].merge(:user_id => current_user.id)) if @project.save redirect_to project_path(@project), :flash => {:success => 'We have created your project'} else redirect_to :back, :flash => {:error => 'Cannot allow an empty project name'} end end this will create a project, from what I understand based on and related

problems showing only projects that belong to a user

♀尐吖头ヾ 提交于 2020-02-02 13:33:07
问题 So I am building a rails app where you can display projects and so on and so forth. I have the following code in my projects controller: def create @project = Project.create(params[:project].merge(:user_id => current_user.id)) if @project.save redirect_to project_path(@project), :flash => {:success => 'We have created your project'} else redirect_to :back, :flash => {:error => 'Cannot allow an empty project name'} end end this will create a project, from what I understand based on and related

problems showing only projects that belong to a user

99封情书 提交于 2020-02-02 13:32:11
问题 So I am building a rails app where you can display projects and so on and so forth. I have the following code in my projects controller: def create @project = Project.create(params[:project].merge(:user_id => current_user.id)) if @project.save redirect_to project_path(@project), :flash => {:success => 'We have created your project'} else redirect_to :back, :flash => {:error => 'Cannot allow an empty project name'} end end this will create a project, from what I understand based on and related

Rails find_or_create_by with/without where

孤人 提交于 2020-02-02 06:50:29
问题 Let's say I have a model named Task . And I want to find_or_create_by some task. t = Task.where(done: false).find_or_create_by(title: 'epic') This model works, but create a task with title equal to epic and done equal to false. I want query search through done equal to false, but I don't want new record done equal to false. How can I do it? 回答1: You can use something called: find_or_initialize_by. It only initializes the record, but doesn't create it. This way, you can override the properties

Rails find_or_create_by with/without where

最后都变了- 提交于 2020-02-02 06:49:27
问题 Let's say I have a model named Task . And I want to find_or_create_by some task. t = Task.where(done: false).find_or_create_by(title: 'epic') This model works, but create a task with title equal to epic and done equal to false. I want query search through done equal to false, but I don't want new record done equal to false. How can I do it? 回答1: You can use something called: find_or_initialize_by. It only initializes the record, but doesn't create it. This way, you can override the properties

Rails ActiveRecord: pretty errors when deleting dependent entities with foreign keys constraints

╄→尐↘猪︶ㄣ 提交于 2020-02-02 05:35:06
问题 I have in Rails application several tables with foreign keys constraints. For example, every order belongs to a customer. There's a costumer_id column on the orders table. When I delete a costumer with a placed order, because of database constraints, MySQL returns the error: Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fails ( orders , CONSTRAINT orders_ibfk_2 FOREIGN KEY ( customer_id ) REFERENCES customers ( id )) And the ugly error pops up on the screen,

Convert Array#select to active record query in rails 4

拥有回忆 提交于 2020-02-01 07:49:22
问题 I'm writing a custom search function, and I have to filter through an association. I have 2 active record backed models, cards and colors with a has_many_and_belongs_to, and colors have an attribute color_name As my DB has grown to around 10k cards, my search function gets exceptionally slow because i have a select statement with a query inside it, so essentially im having to make thousands of queries. i need to convert the array#select method into an active record query, that will yield the