model-associations

Refactoring has_many with scopes

半世苍凉 提交于 2019-12-06 05:47:06
I'm a newbie and I just showed my code to an expert, that told me I shouldn't use has_many to filter my variables, but scopes . I have three models : User, Product and Ownership. So here is my code in app/models/user.rb : class User has_many :ownerships, foreign_key: "offerer_id", dependent: :destroy has_many :owned_products, through: :ownerships, source: :product has_many :future_ownerships, -> { where owning_date: nil, giving_date: nil }, class_name: "Ownership", foreign_key: "offerer_id" has_many :wanted_products, through: :future_ownerships, source: :product end So I deleted the has_many

Source Reflection Errors with has_many :through

我的梦境 提交于 2019-12-05 13:20:24
I'm attempting to create a system where my site's users can favorites pages. Those pages have two types, either clubs or sports. So, I have four models, associated as such: User Model: class User < ActiveRecord::Base .. has_many :favorites has_many :sports, :through => :favorites has_many :clubs, :through => :favorites .. end Favorites Model: class Favorite < ActiveRecord::Base .. belongs_to :user belongs_to :favoritable, :polymorphic => true end Club Model: class Club < ActiveRecord::Base .. has_many :favorites, :as => :favoritable has_many :users, :through => :favorites def to_param slug end

Rails4 Dynamic Select Dropdown

↘锁芯ラ 提交于 2019-12-05 05:37:20
问题 I am trying to set up some dynamic Dropdown Select Menus in a Search Form using form_tag. What I would like is similar functionality to the example found at Railcasts #88 Models: class Count < ActiveRecord::Base belongs_to :host end class Host < ActiveRecord::Base belongs_to :site has_many :counts end class Site < ActiveRecord::Base belongs_to :state has_many :hosts end class State < ActiveRecord::Base has_many :sites end View: <%= form_tag(counts_path, :method => "get", id: "search-form") do

has_one nested attributes not saving

可紊 提交于 2019-12-04 13:34:53
I have two models Project and ProjectPipeline. I want to create a Project form that also has fields from the ProjectPipeline model. I have created the form successfully but when I hit save the values aren't stored on the database. project.rb class Project < ActiveRecord::Base has_one :project_pipeline accepts_nested_attributes_for :project_pipeline self.primary_key = :project_id end projectpipeline.rb class ProjectPipeline < ActiveRecord::Base belongs_to :project, autosave: :true validates_uniqueness_of :project_id end I don't always want a project pipeline but under the right conditions based

Find by conditions on associated model in CakePHP 3

吃可爱长大的小学妹 提交于 2019-12-04 12:44:13
I have two tables orders and sub_orders . Their association is $orders->hasMany('SubOrders', [ 'foreignKey' => 'order_id' ]); Both tables have invoice_no and sub_invoice columns in orders and sub_orders respectively. I have to find records from orders table containing associated sub_orders where $trackingId will match either Orders.invoice_no or SubOrders.sub_invoice $findOrder = $this->Orders->find('all', [ 'conditions' => [ 'OR' => [ 'Orders.invoice_no' => $trackingId, 'SubOrders.sub_invoice' => $trackingId ] ], 'contain' => [ 'SubOrders' ] ]); But this gives error Column not found: 1054

Multiple Associations to the Same Model in CakePHP 3

我只是一个虾纸丫 提交于 2019-12-04 11:08:49
问题 I am using cakePHP version 3.x. When i query the MessagesTable i want to get the Users data for the the sender and the receiver of the message. I have done this many times in cakePHP 2 but i cant figure out why it isn't working in version 3.x. i have a UsersTable and a MessagesTable. UsersTable $this->hasMany('Messages'); MessagesTable $this->belongsTo('Users', [ 'foreignKey' => 'sender_id', 'propertyName' => 'Sender', ]); $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'propertyName'

the best way to implement a friendship model in rails

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:54:38
问题 I want to implement a user's friends system in my app so i found the rails space solution very nice, the idea there is to create two lines in the Friendships table : the first line for the sender invitation, and the second one for receiver relation between users is setup with has_many association like this: has_many :friendships has_many :friends, :through => :friendships, :conditions => "status = 'accepted'" method for accepting a user as friend is like this : # Accept a friend request. def

Rails - How to manage nested attributes without using accepts_nested_attributes_for?

浪尽此生 提交于 2019-12-03 08:33:30
My problem is I've run into limitations of accepts_nested_attributes_for, so I need to figure out how to replicate that functionality on my own in order to have more flexibility. (See below for exactly what's hanging me up.) So my question is: What should my form, controller and models look like if I want to mimmic and augment accepts_nested_attributes_for? The real trick is I need to be able to update both existing AND new models with existing associations/attributes. I'm building an app that uses nested forms. I initially used this RailsCast as a blueprint (leveraging accepts_nested

Rails 3 - has_and_belongs_to_many

早过忘川 提交于 2019-12-02 14:03:31
问题 I have 2 models - Teacher and Subject . A want to connect them via Join table with name Qualification . It looks like i do something wrong: class Teacher < ActiveRecord::Base has_and_belongs_to_many :subjects, :join_table => "Qualification" end class Subject < ActiveRecord::Base has_and_belongs_to_many :teachers, :join_table => "Qualification" end My migration: class CreateQualificationJoinTable < ActiveRecord::Migration def change create_table :qualification, :id => false do |t| t.integer

has_many :through with has_and_belongs_to_many in Rails

拜拜、爱过 提交于 2019-12-02 12:09:34
问题 In Rails - what is the effect of using has_many :through with has_and_belongs_to_many? Consider having two models - Posts and Tags which have a many-to-many relationship as indicated below: class Tag < ActiveRecord::Base has_many :posts_tag has_and_belongs_to_many :posts end class Post < ActiveRecord::Base has_many :posts_tag has_many :tags, :through => posts_tag end class PostsTag < ActiveRecord::Base belongs_to :tag belongs_to :post end The reason I use has_and_belongs_to_many is because a