associations

How to override value that appears in a dropdown in the rails_admin gem

筅森魡賤 提交于 2019-12-11 02:55:30
问题 I have a model UserDelegation that contains: belongs_to :delegator, :class_name => 'User'#, :conditions => {:order => 'users.last_name ASC, users.first_name ASC'} belongs_to :delegatee, :class_name => 'User', :touch => true#, :conditions => {:order => 'users.last_name ASC, users.first_name ASC'} and in my user model i have this: has_many :delegatees, :through => :user_delegatees, :order => 'users.last_name ASC, first_name ASC' has_many :delegators, :through => :user_delegators, :order =>

Rails has_one association confusion

烈酒焚心 提交于 2019-12-11 02:48:03
问题 I am very new to Rails and I want to create a Person model that has one Address and a Company Model that has one Address and one Person. Here is what I've done so far $ rails generate model Address street:string suburb:string $ rails g scaffold Person name:string address:references $ rails g scaffold Company name:string person:references address:references class Address < ActiveRecord::Base belongs_to :person belongs_to :company end class Person < ActiveRecord::Base has_one :address end class

What is wrong with this polymorphic association?

主宰稳场 提交于 2019-12-11 02:44:05
问题 I have class User::AuthInfo < ActiveRecord::Base self.table_name = "auth_infos" belongs_to :authenticable, polymorphic: true end and class User::Customer < ActiveRecord::Base self.table_name = "customers" has_one :auth_info, as: :authenticable end I am expecting that by doing this: User::Customer.last.auth_info the output should be a record from auth_info . But I see the query is wrong: SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`customer_id` = 8 LIMIT 1 My table has

Rails: How do I transactionally add a has_many association to an existing model?

霸气de小男生 提交于 2019-12-11 02:37:09
问题 Let's imagine I run an imaginary art store with a couple models (and by models I'm referring to the Rails term not the arts term as in nude models) that looks something like this: class Artwork < ActiveRecord::Base belongs_to :purchase belongs_to :artist end class Purchase < ActiveRecord::Base has_many :artworks belongs_to :customer end The Artwork is created and sometime later it is included in a Purchase . In my create or update controller method for Purchase I would like to associate the

Doctrine 2 entity association does not set value for foreign key

拜拜、爱过 提交于 2019-12-11 02:29:29
问题 I'm encountering a problem with a Doctrine 2 entity association. I have a User entity and an Agency entity. One Agency can employ multiple Users (the entities are simplified to show only my problem) User Entity /** * @Entity * @Table(name="users") **/ class User { /** * @Id * @Column(type="integer") * @GeneratedValue * @var integer **/ protected $id; /** * @ManyToOne(targetEntity="Agency", inversedBy="users"}) * @JoinColumn(name="agency_id", referencedColumnName="id") * @var Agency */

ruby on rails TypeError in Users#show - Cannot visit Like

旧巷老猫 提交于 2019-12-11 02:24:53
问题 form that causes the problem: <%= form_for Like.find(post.user.likes), :html => { :method => :delete , :class => "unlike_post_like_form" } do |f| %> Post model belongs_to :user has_many :likes User model has_many :posts has_many :likes Like model belongs_to :post belongs_to :user i keep getting the following error: TypeError in Users#show Cannot visit Like on Like.find(post.user.likes), EDIT: First solution: changing Like.find(post.user.likes), to Like.find(post.user.likes).limit(1), Second

Rails' includes() doesn't work with dynamic association conditions

白昼怎懂夜的黑 提交于 2019-12-11 02:19:26
问题 Working on a multi-tenant app where most of my models will have a tenant_id field so I can ensure read permissions by finding through the association ( current_tenant.applications.find(params[:id]) ): class Application < ActiveRecord::Base belongs_to :tenant has_many :app_questions, :conditions => proc {{:tenant_id => tenant_id}}, :dependent => :destroy end I like how this allows me to elegantly create a new AppQuestion with the tenant_id set automatically: @application = current_tenant

Rails ActiveRecord Association

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 02:17:32
问题 Okay, so here is my question. I have a 3 different models, People, Roles, Client, and Store. Clients have many Stores and can also have many people. Stores have many people. People have various roles. 1 Person can work at multiple stores, and they may have different roles at each store. For example. Joe may be an assistant manager at one store and a manager at another store. What I would like to be able to do is pull the correct roles by doing something like Store.find(1).people.find(1).roles

Sails.js associations

半世苍凉 提交于 2019-12-11 02:14:53
问题 I am beginning with sails.js and I am completely lost with my sql queries. I have the following tables : genres +-----------+--------------+------+-----+ | Field | Type | Null | Key | +-----------+--------------+------+-----+ | id | int(6) | NO | PRI | | name | varchar(100) | NO | | | slug | varchar(255) | NO | | | type | varchar(32) | NO | | | parent_id | int(11) | YES | MUL | +-----------+--------------+------+-----+ genres_radios +----------+--------+------+-----+ | Field | Type | Null |

correct way to find max value with association rails

删除回忆录丶 提交于 2019-12-11 00:58:35
问题 I have the following models: #equipment.rb class Equipment < ActiveRecord::Base belongs_to :odometer_type has_many :odometers end #odometer.rb class Odometer < ActiveRecord::Base belongs_to :equipment # I am currently doing this to find the last mileage entered (which is wrong too cause I want the mileage from the max date entered) def self.current_reading(equipment) all.where(:equipment_id => equipment.id).max(:mileage) end end This looks even worse in the view, like this: = @equipment