associations

CakePHP Query - Conditional Based on Contain Field

折月煮酒 提交于 2020-01-07 05:37:09
问题 I am developing a system that holds reports from customer's computers and displays failures in a list. I am attempting to write a query that locates all systems that have currently failed or have failures in the past. My model for Computers has a field that says last_report_pass that allows me to quickly find computers that failed on the current day. My Reports are associated with a computer ID and has a field called status that says whether it was a pass or fail. I am attempting to write a

Rails, Cucumber: make object and its associations

99封情书 提交于 2020-01-07 03:42:08
问题 I'm using mongoid, machinist 2 and pickle. But I think, that question is more common. I have an Account model: class Account include Mongoid::Document include Mongoid::Timestamps referenced_in :user end and User: class User include Mongoid::Document include Mongoid::Timestamps references_one :account end I have the following scenario(I set reference_one association): Scenario: Client views his account Given a user with id: "4ceede9b5e6f991aef000007" And the following accounts exist: | user_id

Limit simple_form_for associated records number in Ruby on Rails

牧云@^-^@ 提交于 2020-01-07 03:39:08
问题 I have a large psychological test of 251 question. Each user can complete that test many times. So I created Summary model to represent each completion. Each Summary has many Answers. For each Summary I created a form that represents collection of answers, using Slim templater and simple_form gem: = simple_form_for(@summary) do |f| = f.simple_fields_for :answers do |a| .question = a.input :question_id, as: :hidden div= a.object.question.title - if a.object.question.kind_of?

Validating uniqueness of nested data. Using cocoon gem

痴心易碎 提交于 2020-01-06 20:05:38
问题 I currently have a Client model that has_many pricings. Pricings table: create_table "pricings", force: true do |t| t.integer "product_id" t.integer "client_id" t.decimal "unit_price" t.datetime "created_at" t.datetime "updated_at" end Pricings model: class Pricing < ActiveRecord::Base belongs_to :client belongs_to :product validates :unit_price, presence: true end Client model: class Client < ActiveRecord::Base has_many :deliveries has_many :collections has_many :pricings accepts_nested

Validating uniqueness of nested data. Using cocoon gem

淺唱寂寞╮ 提交于 2020-01-06 20:01:52
问题 I currently have a Client model that has_many pricings. Pricings table: create_table "pricings", force: true do |t| t.integer "product_id" t.integer "client_id" t.decimal "unit_price" t.datetime "created_at" t.datetime "updated_at" end Pricings model: class Pricing < ActiveRecord::Base belongs_to :client belongs_to :product validates :unit_price, presence: true end Client model: class Client < ActiveRecord::Base has_many :deliveries has_many :collections has_many :pricings accepts_nested

incorrect database records created for rails 3 has_many :through association

自古美人都是妖i 提交于 2020-01-06 19:45:13
问题 I have a has_many :through association. Players have many Teams and Teams have many Players. The join model, Affiliation, belongs to Players and Teams, and also has a year attribute to keep track of a player's team affiliation (or employment) from year to year. I can't seem to figure out the right way to build an association based on the following rules: Create a new player. Associate a team that may be new or existing. So find it or create it, but only create it if the player is saved. The

Rails: Associate posts with user and location

这一生的挚爱 提交于 2020-01-06 08:56:13
问题 I have a Post model in my app, which belongs_to a User model, as well as a Location model. Both the User and Location models use the has_many relation with Post . Now, in my Post controller's create action, I want to automatically associate the Post with the currently logged in user (available to me as current_user through Devise). Also, it should associate with a pre-existing Location if one exists with the same address field (which he enters through the form), or create a new one and

How do I reference my Friendships table to conditionally display code?

你说的曾经没有我的故事 提交于 2020-01-06 05:39:05
问题 I have successfully set up a friendship self referencial association for users in my Ruby on Rails app after following Ryan Bates' railscast. I can successfully follow, unfollow, and view who is following. Now I want to pull in another element and I am not sure how to do it. When on my current_user dashboard page I want to conditionally display an element based on whether or not the current_user is following anyone and if the current_user is being followed by anyone. In oother words, if the

Creating multiple participants in the User model

旧街凉风 提交于 2020-01-06 01:05:27
问题 I'm trying to set up a complex association where the User model is used two times. Once to create the leader of a discussion and then to create the participants for the discussion. First, do I have the associations correct in order to call multiple participants (users) for a discussion led by one user (leader)? User has_many :discussions, foreign_key: :leader_id belongs_to :received_discussions, class_name: 'Discussion' attr_accessible :participant_id Discussion belongs_to :leader, class_name

Rails 4 Create Associated Object on Save

↘锁芯ラ 提交于 2020-01-05 21:13:13
问题 How can I create multiple associated objects automatically just after I save a new primary object? For example In Rails 4, I have three objects: Businesses , Budgets , and Categories . #app/models/business.rb class Business < ActiveRecord::Base #attrs id, name has_many :budgets end #app/models/budget.rb class Budget < ActiveRecord::Base #attrs id, business_id, department_id, value belongs_to :business belongs_to :category end #app/models/category.rb class Category < ActiveRecord::Base #attrs