has-many

Rails has_many with alias name

安稳与你 提交于 2019-11-27 09:01:41
问题 In my User model I could have: has_many :tasks and in my Task model: belongs_to :user Then, supposing the foreign key 'user_id' was stored in the tasks table, I could use: @user.tasks My question is, how do I declare the has_many relationship such that I can refer to a User's Tasks as: @user.jobs ... or ... @user.foobars Thanks a heap. 回答1: Give this a shot: has_many :jobs, foreign_key: "user_id", class_name: "Task" Note, that :as is used for polymorphic associations. 回答2: You could also use

How to create NHibernate HasManyToMany relation

柔情痞子 提交于 2019-11-27 02:17:34
I know there are questions about HasManyToMany but this time I want to put couple fields into middle table like 'Description, CreationDate'. For my situation I don't want to bind two way. I have company, person and address tables. And every company or person may have more than 1 address. In this situation what should I do? How should I write the code of classes and mappings? Below you can see the tables: Radim Köhler In this case the answer is pretty simple. Do not use many-to-many. Use pairing object. Exactly for the reasons you've mentioned: Extend the pairing object with more properties:

Need data from rails join table, has_many :through

旧巷老猫 提交于 2019-11-26 22:54:52
问题 I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id . This would mean: class User has_many :things, :through => :follows end class Thing has_many :users, :through => :follows end class Follow belongs_to :users belongs_to :things end So I can retrieve thing.users with no problem. My issue is if in the follows table, I have a column named "relation", so I can set a follower as an "admin", I want to have access

Validate the number of has_many items in Ruby on Rails

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:04:45
Users can add tags to a snippet: class Snippet < ActiveRecord::Base # Relationships has_many :taggings has_many :tags, :through => :taggings belongs_to :closing_reason end I want to validate the number of tags: at least 1, at most 6. How am I about to do this? Thanks. Nikita Rybak You can always create a custom validation . Something like validate :validate_tags def validate_tags errors.add(:tags, "too much") if tags.size > 5 end sbonami A better solution has been provided by @SooDesuNe on this SO post validates :tags, length: { minimum: 1, maximum: 6 } asukiaaa I think you can validate with

Rails Model has_many with multiple foreign_keys

我的未来我决定 提交于 2019-11-26 21:58:24
Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a has_many (the first gets overwritten). class Person < ActiveRecord::Base belongs_to :father, :class_name => 'Person' belongs_to :mother, :class_name => 'Person' has_many :children, :class_name => 'Person', :foreign_key => 'mother_id' has_many :children, :class_name => 'Person', :foreign_key => 'father_id' end Is there a simple way to use has_many with

Rails has_many :through Find by Extra Attributes in Join Model

拟墨画扇 提交于 2019-11-26 17:07:04
New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ActiveRecord::Base has_many :event_users has_many :events, :through => :event_users end class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user #For clarity's sake, EventUser also has a boolean column "active", among others end class Event < ActiveRecord::Base has_many :event_users has_many :users, :through => :event_users end This project is a calendar, in which I have to keep track of people signing

How to create NHibernate HasManyToMany relation

怎甘沉沦 提交于 2019-11-26 10:04:48
问题 I know there are questions about HasManyToMany but this time I want to put couple fields into middle table like \'Description, CreationDate\'. For my situation I don\'t want to bind two way. I have company, person and address tables. And every company or person may have more than 1 address. In this situation what should I do? How should I write the code of classes and mappings? Below you can see the tables: 回答1: In this case the answer is pretty simple. Do not use many-to-many. Use pairing

Validate the number of has_many items in Ruby on Rails

那年仲夏 提交于 2019-11-26 08:09:15
问题 Users can add tags to a snippet: class Snippet < ActiveRecord::Base # Relationships has_many :taggings has_many :tags, :through => :taggings belongs_to :closing_reason end I want to validate the number of tags: at least 1, at most 6. How am I about to do this? Thanks. 回答1: You can always create a custom validation. Something like validate :validate_tags def validate_tags errors.add(:tags, "too much") if tags.size > 5 end 回答2: A better solution has been provided by @SooDesuNe on this SO post