associations

Rails - one model with two associations

坚强是说给别人听的谎言 提交于 2019-12-06 15:49:17
I have the model Member that contains all informations about the registered member on my site. Then I have the model Message which contains 2 columns (actually 3 with id ): - member_id - message_from In the table Messages are stored IDs of user, how chatting together - when the member A send message to member B , so in the column member_id is saved ID of person A and into the column message_from ID of the person B . My current associations looks like: class Member < ActiveRecord::Base has_many :messages_from end class Message < ActiveRecord::Base belongs_to :member end I don't know, how could

Rails, setting up favorites between two models

落爺英雄遲暮 提交于 2019-12-06 15:15:27
I followed the steps from here to setup a favorites relationship between a user model and a company model. Specifically, a user can have many favorite companies. There is also a functionality of a user being tied to a company on creation. So here are my current models/routes/db schema Models class FavoriteCompany < ActiveRecord::Base belongs_to :company belongs_to :user ... class User < ActiveRecord::Base has_many :companies has_many :favorite_companies has_many :favorites, through: :favorite_companies ... class Company < ActiveRecord::Base belongs_to :user has_many :favorite_companies has

Rails with Devise: Problems with associations

给你一囗甜甜゛ 提交于 2019-12-06 14:15:42
问题 I have made new Rails app. It is totally fresh with just one User model (that is generated through Devise) and a Post model that is generated with scaffold. In the Post model I have a column in the database that is named user_id . The problem is that user_id in the Post table always is nil (it won't change to the user_id of the user that is posting). I suspect that is has something to do with Devise, but I'm not totally sure. Any suggestions on what to do? user.rb class User < ActiveRecord:

Many-to-many connection and Associations

牧云@^-^@ 提交于 2019-12-06 13:17:02
问题 A User can Upload a Clip via the Clips Scaffold , this clip either belongs to a Show and or an Album (Show Controller, Album Controller). A user can have ONLY ONE SHOW MANY ALBUMS What i'm trying to accomplish: A User can Upload Clips , these clips can be attached to either a Show or/and an Album For a more detailed overview take a look at the image below: Problem #1 What i'm not understanding is the associations i must use. My current Models: User Model : has_many :clips, dependent: :destroy

Performing multiple joins on the same association with Squeel

爱⌒轻易说出口 提交于 2019-12-06 13:15:34
In my application, I have to models: Workflow and Step; steps belongs_to workflow and a workflow has_many steps. Steps have an index and a boolean status ('completed'). I want to retrieve workflows whose step 1 is completed and step 2 is not, i.e. something like this in SQL: SELECT * FROM workflows w INNER JOIN steps s1 ON s1.workflow_id = w.id INNER JOIN steps s2 ON s2.workflow_id = w.id WHERE s1.index = 1 AND s1.completed = 1 AND s2.index = 2 AND s2.completed = 0 I've tried to express this query with Squeel, but it seems it does not allow multiple joins on the same association: I cannot find

Disabling a FactoryGirl association within a trait

瘦欲@ 提交于 2019-12-06 12:04:08
In a Rails app, I'm using FactoryGirl to define a general factory plus several more specific traits. The general case and all but one of the traits have a particular association, but I'd like to define a trait where that association is not created/built. I can use an after callback to set the association's id to nil , but this doesn't stop the association record from being created in the first place. Is there a way in a trait definition to completely disable the creation/building of an association that has been defined for the factory the trait belongs to? For example: FactoryGirl.define do

Sequelize 'hasMany' associated(model) count in attributes in query execution

自作多情 提交于 2019-12-06 11:32:08
问题 Sequelize version: 4.22.6, MySql version:5.7.8 I want to 'hasMany' associated(CompanyUser) count in attibutes(at place of _user_count_) in query execution /** * Company user associate with Company with belongsTo relation */ `CompanyUser.belongsTo(Company, { foreignKey: 'company_id', targetKey: 'id'});` /** * Company associate with Company user with hasMany relation */ `Company.hasMany(CompanyUser, { foreignKey: 'company_id', sourceKey: 'id'});` `return Company.findAll({ attributes: [ 'id',

How do I create custom “association methods” in Rails 3?

南笙酒味 提交于 2019-12-06 10:36:28
问题 I've read this article, but it's for Rails 1.x. I'd really like to create my own association methods: user = User.find(1) # Example of a normal association method user.replies.create(:body => 'very informative. plz check out my site.') # My association method user.replies.find_by_spamminess(:likelihood => :very) In Rails 3, what's the proper way of doing this? 回答1: The Rails 3 way of doing things is often to not use find methods, but rather scopes, which delays the actual database call until

How to disable belongs_to :touch option in Rspec tests for Rails models?

℡╲_俬逩灬. 提交于 2019-12-06 10:31:14
问题 Having a large model stack and using doll caching techniques extensively, one ends up with lots of parent models been "touched" after a model update. While testing, this seems to be a time waster unless you try to test that feature specifically. Is there a way to prevent models to touch their belongs_to associations for the test environment or at a test level? UPDATE 1: My first attempt to the case would be to # /config/initializers/extensions.rb # class ActiveRecord::Base def self.without

How do I get IDropTarget to work with my Drop Handler in Delphi?

限于喜欢 提交于 2019-12-06 09:51:06
I have associated a file extension with my Delphi 2009 program. I have been using the command line call method to pass the filename to my Delphi program so it can be opened. However, I found that when selecting multiple files, and clicking on them all at once, it opens each file in a separate instance of my program. I asked about this , and apparently the solution is to use one of the other two Windows methods: DDE or IDropTarget . But DDE is being deprecated, and MSDN recommends the IDropTarget method. Also Lars Truijens in his answer to me , says that IDropTarget might fit better if I'm