activerecord

A has_many Bs where B has no primary key

China☆狼群 提交于 2019-12-10 14:28:43
问题 I've got models A and B; A has_many B, and B belongs_to A. So far, so good—except, I specify that B doesn't have a primary key. I don't plan to ever modify or delete individual B rows, and I expect to have several million to billion of them, so omitting the primary key will be really handy, space-wise. The migration to create B's table looked like this: class CreateBs < ActiveRecord::Migration def change create_table :bs, {:id => false} do |t| # … rest of fields … end end end Unfortunately,

Re sorting active records in rails without re querying the database?

孤街醉人 提交于 2019-12-10 14:25:17
问题 For example lets assume I have a model called Products and in the Products controller I have the following code for the product_list view to display products sorted. @products = Product.order(params[:order_by]) And lets imagine in the product_list view the user is able to sort by price, rating, weight etc using a dropdown. The products in the database won't change frequently. What i'm having a hard time understanding is whether rails will have to query each time when the user selects a new

How do I load seed.rb within a Rails test environment using rpec?

烈酒焚心 提交于 2019-12-10 14:11:13
问题 I have the following seeds.rb file: State.create [ {:name => "Alabama", :abbreviation => "AL" }, {:name => "Alaska", :abbreviation => "AK" }, {:name => "Arizona", :abbreviation => "AZ" }, {:name => "Arkansas", :abbreviation => "AR" }, {:name => "California", :abbreviation => "CA" }, {:name => "Colorado", :abbreviation => "CO" }, {:name => "Connecticut", :abbreviation => "CT" }, {:name => "Delaware", :abbreviation => "DE" }, {:name => "District Of Columbia", :abbreviation => "DC" }, {:name =>

Data Modeling 3 Way Table has_many association

匆匆过客 提交于 2019-12-10 14:05:42
问题 I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids

ruby(1.9.3) on rails(3.2.3) Activerecord-odbc-adapter

吃可爱长大的小学妹 提交于 2019-12-10 13:54:32
问题 I have a legacy database(Progress OpenEdge) that i need to use for a myriad of reports. I have everything working with ruby 1.8.6 and rails 2.0 (odbc-adapter and odbc-rails). Since 1.8.6 is not supported anymore and rails 2.0 is quite old i'd like to update. Is there a odbc-adapter supported on 3.2.3? Any input is appreciated. Thanks, 回答1: You have to put the following line in your gemfile: gem 'ruby-odbc' and then run bundle install. i think this is what you were looking for :) 回答2: I am

ActiveRecord: How to find parents whose ALL children match a condition?

萝らか妹 提交于 2019-12-10 13:52:21
问题 Suppose I have a Parent model that has many Child , and that Child also belongs to OtherParent . How can i find all Parent where ALL of its Child belongs to any OtherParent ? In pure SQL I could do Parent.find_by_sql(<<SQL) SELECT * FROM parents p WHERE NOT EXISTS ( SELECT * FROM children WHERE parent_id = p.id AND other_parent_id IS NULL ) SQL (from here), but I'd prefer to do it by taking advantage of ActiveRecord if possible. Thanks! I'm using Rails 4.2.1 and PostgreSQL 9.3 回答1: Using arel

How to display a form for a subset of associated records, some of which don't exist yet?

拈花ヽ惹草 提交于 2019-12-10 13:48:16
问题 I have Tasks and Users. When a user completes a task, I create a Completion which has a field for the user to indicate how long they spent. I need a form that shows all the tasks with their completion status and time_spent attribute. On submit, completions that existed should be updated and new ones should be created. I'd like to do this in Formtastic if possible but I'll be happy with a basic Rails 3 solution. class Completion < ActiveRecord::Base belongs_to :task belongs_to :user #

Rails Model inheritance in forms

喜你入骨 提交于 2019-12-10 13:30:15
问题 I'm doing a reporting system for my app. I created a model ReportKind for example, but as I can report a lot of stuff, I wanted to make different groups of report kinds. Since they share a lot of behavior, I'm trying to use inheritance. So I have the main model: model ReportKind << ActiveRecord::Base end and created for example: model UserReportKind << ReportKind end In my table report_kinds I've the type column, and until here its all working. My problem is in the forms/controllers. When I

has_one through association with condition

ε祈祈猫儿з 提交于 2019-12-10 13:20:55
问题 I have 3 relevant tables/models. I would like to retrieve the user which has organized a party , given a Party record, a join table with a boolean organized column, and a users table. My best attempt so far (this one makes the most sense to me after much fiddling). I've omitted the irrelevant columns. class Party # party_id has_many :parties_users has_many :users, through: :parties_users, source: :user has_one :organizer, -> { where organizer: true }, through: :parties_users, source: :user

Ruby on Rails: why ActiveRecord uses “datetime” type instead of “timestamp” for timestamp fields?

柔情痞子 提交于 2019-12-10 13:04:49
问题 In my database migration file I inserted the line: t.timestamps Two columns, as I expected, were created: "updated_at" and "created_at". However, their type is "datetime" and not "timestamp". I am using MySQL and the "timestamp" type, as I understand, is designed exactly for such cases, as it uses less space and is independent of timezone. So, is there any reason, why Rails 3 uses "datetime" and not "timestamp"? Should I try to fix that? If yes, is there any way to do this besides not using