activerecord

How to use Rails include?

╄→尐↘猪︶ㄣ 提交于 2019-12-13 10:53:54
问题 I have a model user with many orders class User < ApplicationRecord has_many :orders, inverse_of: :user, dependent: restrict_with_exception end and a model order as follows :- Class Order < ApplicationRecord belongs_to :user, inverse_of: :order end I want to fetch all orders but with details of user like user.name and user.mobile in same set. How do I use include in this case? 回答1: You can use includes using the below mentioned query: @users = User.where(id: 1).includes(:orders) then iterate

Ruby on rails active record associations

本小妞迷上赌 提交于 2019-12-13 09:37:45
问题 I need 2 models for my store: Shoe ShoeSize According to my concept - one shoe can have several sizes (one model of shoes can be 34,35,36,37 size). What types of associations should I use? What database table fileds do I need to create to support these associations? Here is an example of the models in use: Shoe.find(1).shoe_sizes => 34,35,36 ShoeSize(2).shoes => #Shoe1, #Shoe2, #Shoe5 回答1: You should use a has_and_belongs_to_many relationship. class Shoe has_and_belongs_to_many :shoe_sizes

Extract records which satisfy a model function in Rails

 ̄綄美尐妖づ 提交于 2019-12-13 08:34:44
问题 I have following method in a model named CashTransaction . def is_refundable? self.amount > self.total_refunded_amount end def total_refunded_amount self.refunds.sum(:amount) end Now I need to extract all the records which satisfy the above function i.e records which return true . I got that working by using following statement: CashTransaction.all.map { |x| x if x.is_refundable? } But the result is an Array . I am looking for ActiveRecord_Relation object as I need to perform join on the

Using Plucked Values in Ruby on Rails

末鹿安然 提交于 2019-12-13 08:28:03
问题 Given a model named Album, I want to use the values I pluck. For starters, I want to try to print the values. How can I do this? Here is my current code: my_arr = Album.where(title: "Greatest Hits", artist: "The Beatles").pluck(:publisher, :year_published) puts my_arr[0][0].to_a To make things even simpler, how would I retrieve any value at all, so that I could place it into a normal variable? I'm using Rails 4 with SQLite3. 回答1: pluck already returns an array - you do not need to manipulate

Rails/ActiveRecord/SQLite3: Can't save records in test environment

时间秒杀一切 提交于 2019-12-13 07:25:59
问题 I have a Rails app using sqlite3. Anytime I want to save anything in test environment I'm getting the following error: ActiveRecord::StatementInvalid: SQLite3::SQLException: near "SAVEPOINT": syntax error: SAVEPOINT active_record_1 In development, everything works fine. I'm using Rails 3.2.7. 回答1: You should upgrade your OS default version of Sqlite http://shynnergy.com/2012/07/savepoint-exception-with-rails-3-1-on-jenkins/ 来源: https://stackoverflow.com/questions/12017182/rails-activerecord

Can access _id of a references object but not the object directly

旧城冷巷雨未停 提交于 2019-12-13 07:13:39
问题 User has_many :organizations create_table "organizations", :force => true do |t| t.string "name" t.integer "founder_id" I can assign founder_id, but not access Founder (method missing). class CreateOrganizations < ActiveRecord::Migration def change create_table :organizations do |t| t.string :name t.belongs_to :founder, :class_name => "User" What do I need to change so I can access the founder (a User) attr on an Oranization? 回答1: Method belongs_to should be called on Organization class, like

Ruby on Rails Redmine Active Record::Record not found for select pages

五迷三道 提交于 2019-12-13 07:13:30
问题 I've recently started at a new company which uses Redmine and I've been asked to upgrade from 2.3.3 to 3.0.3 - complete novice with Ruby and Rails but it seems OK so far. I've installed an instance of 3.0.3 and updated the database using mysqldump. It seems to have largely worked, however random wiki pages on the new Redmine don't work - simply get a 404 error in the browser. Everything else has succeeded - all user credentials, config for the application, many projects, issues etc, and in

Attribute available before_type_cast but nil when accessed directly

谁说胖子不能爱 提交于 2019-12-13 07:13:26
问题 I have an ActiveRecord object that has a before_create hook that generates a SHA hash and stores the result in an attribute of the object: before_create :store_api_id attr_reader :api_id def store_api_id self.api_id = generate_api_id end private def generate_api_id Digest::SHA1.hexdigest([Time.now.nsec, rand].join).encode('UTF-8') end This works in that the api_id attribute is created and stored in the database as text (which is forced by the call to .encode('UTF-8') otherwise SQLite will try

How to override CActiveDataProvider Method to show CGridview in descending order by default in Yii

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 07:07:40
问题 I have many models and gridviews in my project and every model has create_dttm field. I would like now that how can I override class CActiveDataProvider where I'll add DESC Sort condition i.e. 'sort' => array( 'defaultOrder' => 'create_dttm DESC', ) so that it'll work for all my gridviews instead of adding condition in each gridview. Example: public function search() { $criteria = new CDbCriteria; $criteria->compare('id', $this->id); $criteria->compare('name', $this->name, true); $criteria-

Rails4 // append strong_parameters with other params

亡梦爱人 提交于 2019-12-13 06:59:10
问题 Let's say for the following actions' controller: class PostsController < ApplicationController def create @post = Post.create(post_params) end private def post_params params.require(:post).permit(:title, :content) end end Is there a one-line way to do something like this when creating a record : def create @post = Post.create(post_params, user_id: current_user.id) end What would be the clean way to do it ? Is it possible ? 回答1: params is an instance of ActionController::Parameters, which