rails-activerecord

How to create fixtures with foreign key alias in rails?

妖精的绣舞 提交于 2019-12-21 22:02:28
问题 I have two models, App and User , where an App has a creator who is a User . # app.rb class App < ActiveRecord::Base belongs_to :creator, class_name: 'User' end # user.rb class User < ActiveRecord::Base has_many :apps, foreign_key: "creator_id" end How do I create fixtures for this? I tried: # apps.yml myapp: name: MyApp creator: admin (User) # users.yml admin: name: admin But this doesn't work, since the relation is an aliased foreign key, not a polymorphic type. Omitting the (User) in the

Rails 3 Query: getting error while using 'select' with 'order'

允我心安 提交于 2019-12-21 21:26:20
问题 I was trying to answer this question where I got this issue. I have a user model having id , email and first_name columns. So in single query I want to select users with distinct first_name , sort them by email and pluck their ID . How can I do this? what won't work: User.select(:first_name).uniq.pluck(:id) because it fires this SQL SELECT DISTINCT "users"."id" FROM "users" . This is selecting distinct id from user. But we want to select distinct first_name User.order("email DESC").pluck(:id)

Rails with Postgres data is returned out of order

自古美人都是妖i 提交于 2019-12-21 18:33:47
问题 I've just transitioned my app from MySQL to Postgres. Previously, a request for .all returned all the rows in id order. On Postgres the rows are returned out of order. Likewise, Person.first used to return the record with id 1, now it sometimes returns another record. If I add an order clause like this: Person.order("id").first The query succeeds and returns the first row. Is this expected behaviour? 回答1: this post answers your question: I don't think ordering by ID is guaranteed by default,

ActiveRecord :includes - how to use map with loaded associations?

浪子不回头ぞ 提交于 2019-12-21 18:31:12
问题 I have a small rails app, and I'm trying to get some order statistics. So I have an Admin model, and an Order model, with one-to-many association. class Admin < ActiveRecord::Base attr_accessible :name has_many :orders class Order < ActiveRecord::Base attr_accessible :operation belongs_to :admin And I'm trying to get specifical orders using this query: admins = Admin.where(...).includes(:orders).where('orders.operation = ?', 'new gifts!') That works just as expected. But when I try to make

Difference between ActiveRecord and ActiveRecord::Relation objects

半腔热情 提交于 2019-12-21 14:51:32
问题 I have searched but not able to find the brief explanation for the difference between ActiveRecord and ActiveRecord::relation object. I have understand that ActiveRecord is the single object find by something like User.find(1) And ActiveRecord::Relation is the array like object Find by something like User.where(id: 1) I am looking for the difference between them in terms of query execution or deep explanation about them, so it will clear the whole concept behind it. Thanks in advance! 回答1: An

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 =>

Ruby on Rails ActiveRecord scopes vs class methods

那年仲夏 提交于 2019-12-21 07:01:05
问题 Rails internally converts scopes to class methods then why can't we use class methods itself instead of going with scopes. 回答1: From the fine guide: 14 Scopes [...] To define a simple scope, we use the scope method inside the class, passing the query that we'd like to run when this scope is called: class Article < ActiveRecord::Base scope :published, -> { where(published: true) } end This is exactly the same as defining a class method, and which you use is a matter of personal preference:

Problems while making a generic model in Ruby on Rails 3

百般思念 提交于 2019-12-21 05:44:05
问题 I'm trying to make a "generic model" so it can connect to any table of any database. First, I made this class which connects to another database specified (not using the schema) Db class Db < ActiveRecord::Base self.abstract_class = true attr_accessor :error def initialize(item = nil) @error = "" connect super end def connect could_connect = true @error = "" begin ActiveRecord::Base.establish_connection( :adapter => "mysql2", :host => "localhost", :username => "root", :password => "",

Share enum declaration values between models

こ雲淡風輕ζ 提交于 2019-12-20 17:59:31
问题 I'm applying enum on the following attribute: transparency The same attribute (with enum) is used in two different models: Category and Post Is it possible to share the enum values between models, to avoid code duplication: enum transparency: %w(anonymous private public) 回答1: You can use a concern. module HasTransparency extend ActiveSupport::Concern included do enum transparency: %w(anonymous private public) end end Then include it in your models: class Category < ActiveRecord::Base include

Rails includes with scope

时光总嘲笑我的痴心妄想 提交于 2019-12-20 17:37:48
问题 I have a model called Author. An author has many Articles. Articles have a scope called .published that does: where(published: true). I want to load the author, with the published articles. I tried: Author.includes(:articles.published).find(params[:author_id]) But that throws an error: undefined method 'published'. Any idea? 回答1: I think the best solution would be: Author.includes(:articles).where(:articles=>{published: true}).find(params[:author_id]) Or you can create scope: class Author <