associations

Rails - how to store “has_many” checkboxes association in database?

天涯浪子 提交于 2019-12-07 08:27:39
问题 I have the table User and the table Categories . The relation between these two tables: user has_many :categories category belongs_to :user I would like to display on the user's (standard) edit page the list of categories with checkboxes. When this user would check some of the checkboxes, I would like to save them and then display as checked when he open the page the next time. But how to render the checkboxes in the view? And how to store information about what checkboxes the user checked in

Association Injection or Dependency Injection?

五迷三道 提交于 2019-12-07 08:25:21
问题 I'm studying the dependency injection pattern. I have read many examples, among which one typical example is to use XxxService/XxxRepository as example. But from my opinion, according to the UML concept, class XxxRepository should be an association of class XxxService. Why not call this situation Association Injection , but still Dependency Injection?:) Thanks! UPDATE ON 1/26/2018 I currently think the concept of dependency injection is suitable for the situation described in this question.

Deleting Records From Associated Table With PatchEntity in CakePHP

安稳与你 提交于 2019-12-07 07:53:28
I have a CakePHP model where a Job hasmany Employees and an Employee belongsTo a Job. The View I use to edit both Jobs and Employees is a Job view. Here is code from my Job Controller: $job = $this->Jobs->get($id, [ 'contain' => ['Employees'] ]); if ($this->request->is(['patch', 'post', 'put'])) { $req = $this->request->data; $job = $this->Jobs->patchEntity($job, $req, [ 'validate' => false, 'associated' => ['Employees'] ]); $saveResult = $this->Jobs->save($job, [ 'validate' => false, 'associated' => ['Employees'] ]); $this->request->data looks like this: 'employees' =>[ 0 => [ 'id' => 1, ...

When to use an attribute (property) instead of an association/aggregation/composition when drawing a UML

坚强是说给别人听的谎言 提交于 2019-12-07 07:37:32
问题 Okay so I'm a bit confused as to when I should add an attribute to a class vs drawing an association to a class when trying to show a relationship in a UML diagram. For example let's say I have a DFA class that contains 10 state objects each having a different set of paths to various states in the DFA. Should I draw the composition line from the DFA to the State class or just type all 10 states in the attribute section of the DFA class. Basically I'm trying to figure out if when a Class A

Is it possible to use a scope as source for association with simple_form?

我的未来我决定 提交于 2019-12-07 03:57:26
Is it possible to use a scope as source for association? class User < AR scope :active_users, where('status = 4') ... # form <%= f.association :active_users %> ... Sorry, I don't think that will work. A scope returns an ActiveRecord::Relation and simple_form is looking for a symbol that represents an existing ActiveRecord::Association (E.g. has_many, belongs_to). https://github.com/plataformatec/simple_form#associations You can use collection for source in controller @active_users = User.active_users in view <%= f.association :active_user, collection: @active_users %> 来源: https://stackoverflow

Ruby on Rails 3: Combine results from multiple has_many or has_many_through associations

可紊 提交于 2019-12-07 03:40:17
问题 I have the following models. Users have UserActions, and one possible UserAction can be a ContactAction (UserAction is a polymorphism). There are other actions like LoginAction etc. So class User < AR::Base has_many :contact_requests, :class_name => "ContactAction" has_many :user_actions has_many_polymorphs :user_actionables, :from => [:contact_actions, ...], :through => :user_actions end class UserAction < AR::Base belongs_to :user belongs_to :user_actionable, :polymorphic => true end class

Rails: has_many through not returning correctly with namespaced models

北城以北 提交于 2019-12-07 02:38:27
问题 I have 3 models. Rom::Favorite , Rom::Card , User . I am having an issue creating the User has_many rom_cards through rom_favorites Here are my relevant parts of my models Rom::Card class Rom::Card < ActiveRecord::Base has_many :rom_favorites, class_name: "Rom::Favorite", foreign_key: "rom_card_id", dependent: :destroy self.table_name = "rom_cards" end User class User < ActiveRecord::Base # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password

Creating a nested JSON request with Rails

泄露秘密 提交于 2019-12-06 16:13:08
问题 I have a bit of a complex situation. I am building an iPhone app with a rails backend. There are two model objects, a chat session and a person. They are defined as follows: class Person < ActiveRecord::Base belongs_to :chatsession end class Chatsession < ActiveRecord::Base has_many :people belongs_to :leader, :class_name => "Person" end The leader is essentially the main person for that specific chat session. Now from my iPhone app I want to be able to create a Chatsession with the leader in

Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions?

偶尔善良 提交于 2019-12-06 16:09:18
问题 What's the best way to do this? I'm trying something like this, but it feels... wrong. (NOTE: It's all about the :conditions in the has_many association...) class Submission < ActiveRecord::Base belongs_to :checklist has_many :jobs, :through => :checklist, :source => :job, :conditions => ["jobs.archived = false OR jobs.archived_at > #{self.created_at} OR jobs.created_at < #{self.created_at}"] end Rails 3.2.11, Ruby 1.9.3 What I'm trying to do I need to pass multiple conditions on a has_many

Extending windows explorer context menu

China☆狼群 提交于 2019-12-06 15:54:35
I'm having hard times figuring out why this doesn't work on my computer. I've read this article http://msdn.microsoft.com/en-us/library/bb776820.aspx and tried it, and it works for an unknown file type, but for know such as .bmp it doesn't - I've also deleted other keys under .bmp - didn't help. I've tried this in HKEY_CLASSES_ROOT.bmp and in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.bmp I need to implement this in my program so it has custom context menu items on some file types like bmp. btw. I've tried ContextEdit (a freeware program) - also didn't work -