activerecord

Rails: Efficiently searching by both firstname and surname

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 22:08:52
问题 I'm trying to create a 'search box' that matches users by name. The difficulty is that a user has both a firstname and a surname. Each of those can have spaces in them (eg "Jon / Bon Jovi", or "Neil Patrick / Harris"), and I'm wondering about the most efficient way to ensure the search is carried out on a concatenation of both the firstname and surname fields. The list of users is quite large, so performance is a concern. I could just throw a "fullname" def in the user model, but I suspect

accepts_nested_attributes_for and nested_form plugin

半世苍凉 提交于 2019-12-10 21:30:53
问题 I've the following code in a _form.html.haml partial, it's used for new and edit actions. (fyi I use the Ryan Bates' plugin nested_form) .fields - f.fields_for :transportations do |builder| = builder.collection_select :person_id, @people, :id, :name, {:multiple => true} = builder.link_to_remove 'effacer' = f.link_to_add "ajouter", :transportations works fine for the new action... for the edit action, as explain in the doc, I've to add the :id of already existing associations, so, I've to add

How to create a new MySQL database with ActiveRecord in a non-rails app?

眉间皱痕 提交于 2019-12-10 21:25:19
问题 I'm building a non-rails pure ruby app that uses ActiveRecord. I want to write a rake file that creates a database and tables for it. I try the following code namespace :db do task :create do conn = ActiveRecord::Base.connection create_db = "CREATE DATABASE foo_dev" conn.execute(create_db) end end But this gives me ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished error. Well, this is obvious because I didn't connect ActiveRecord to any database. What should I do?

Rails Active Record: How to reduce number of trips to database?

痞子三分冷 提交于 2019-12-10 21:03:36
问题 In my rails project, I have a feature that shows users whether any of their facebook friends are already on the site. After authenticating, I load their facebook friends into @fb_friends and then do this: @fb_friends.each do |friend| friend_find = Authorization.find_by_uid_and_provider(friend[:identifier], 'facebook') if friend_find @fb_friends_on_cody << friend_find.user_id @fb_friends.delete(friend) end end Basically, if a match is found (via the Authorization table), I push that record to

Trying to create a scope in rails 3 for has habtm relationship

别来无恙 提交于 2019-12-10 20:38:01
问题 I'm trying to do a basic search on an active record but am having trouble because of the has_and_belongs_to_many relationship. Below are the records and scopes I've created. Class Section << ActiveRecord::Base has_and_belongs_to_many :teachers attr_accessible :department, :class_size scope :department_scope, lambda { |dept| where("department = ?", dept) } scope :teacher_fname_scope, lambda { |n| joins(:teachers) & Teacher.has_first_name(n) } end Class Teacher << ActiveRecord::Base has_and

How to write Activerecord/Arel intersection query

陌路散爱 提交于 2019-12-10 20:25:32
问题 I got this working in PSQL: SELECT "profiles".id FROM "profiles" INNER JOIN "integration_profiles" ON "integration_profiles"."profile_id" = "profiles"."id" INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" WHERE "integrations"."provider" = 'csv' INTERSECT SELECT "profiles".id FROM "profiles" INNER JOIN "integration_profiles" ON "integration_profiles"."profile_id" = "profiles"."id" INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles

ActiveRecord relations between models on different engines

橙三吉。 提交于 2019-12-10 20:24:08
问题 I'm building a Rails app that is probably going to have a large amount of models. For now let's say that i need to have Users, and each User will have a personal Blog I've been developing the User-related part on a separate Engine (let's call it AuthEngine) that encapsulates the User model, authentication, etc. At the time this seemed like the best approach In parallel, my colleague was developing another engine, to deal with the blog features (let's call it BlogEngine). Now he needs to

Deleting VS Finding Orphans using ActiveRecord helpers

安稳与你 提交于 2019-12-10 19:34:25
问题 I'm trying to delete all the organizations that no longer have any users . Using the below code, I can find all the records I wish to delete: Organization.includes(:users) .where(users: { id: nil }) .references(:users) When I add delete_all , I get the same error I would get if I didn't include references : PG::UndefinedTable: ERROR: missing FROM-clause entry for table "users" I could probably write the solution in pure SQL, but I don't understand why Rails isn't keeping the reference to

rails has_many setter should set conditions if specified

不打扰是莪最后的温柔 提交于 2019-12-10 19:24:38
问题 This seems like a bug in Rails to me, but there's probably not much I can do about that. So how can I accomplish my expected behavior? Suppose we have: class User < ActiveRecord::Base has_many :awesome_friends, :class_name => "Friend", :conditions => {:awesome => true} end And execute the code: >> my_user.awesome_friends << Friend.new(:name=>'jim') Afterwards, when I inspect this friend object, I see that the user_id field is populated. But I would also expect to see the "awesome" column set

how to manage 3 many-to-many models in Rails

∥☆過路亽.° 提交于 2019-12-10 19:19:47
问题 I'm following Railscast Advice of making a different model to maintain many-to-many relationships. However, I am having trouble extracting transitive relation data. Imagine that there are 3 models with many-to-many: User <-> Color <-> Shades I've made 2 more models: ColorLiking (maintains User <-> Color), DiffShades (maintains Color <-> Shades) Question Now, If everything is set up correctly...how do I find the Shades that belong to a User ? How Will I setup that relationship? class User <