has-many-through

Laravel HasManyThrough Keys

非 Y 不嫁゛ 提交于 2019-12-12 01:56:47
问题 I have 3 models: User, Program, UserProgram. UserProgram is an actual model of its own. Here are how the models look in the database: users id programs id user_programs user_id program_id I would like to have in my Program model: function users() { return $this->hasManyThrough('App\User','App\UserProgram'); } But this does not work. How can I make this relationship work? 回答1: hasManyThrough is not used for this purpose. You need a many-to-many relationship. class Users { public function

Has Many Through two models

帅比萌擦擦* 提交于 2019-12-12 01:54:50
问题 I have two models of importance here. There's more models interacting with them too, but only these should be relevant. class Image < ActiveRecord::Base belongs_to :user belongs_to :trip end class Mission < ActiveRecord::Base belongs_to :user belongs_to :trip # has_many :images, through: :user end The commented out line gets me halfway there but I want to meet an additional condition of the image having the same Trip ID as the Mission. # has_many :images, through: [:user, :trip] Using an

Rails 3.1: Custom validation message on join table of has_many through relationship?

妖精的绣舞 提交于 2019-12-12 01:47:00
问题 I have a Meal model that has_many :foods, :through => :servings . Meal also: accepts_nested_attributes_for :servings, :allow_destroy => true validates_associated :servings The Serving model has a field called serving_amount and a field called amount_recorded . In the Serving model, I currently use this validation: validates :serving_size, :numericality => {:greater_than => 0}, :if => :amount_recorded? The message returned if the validation fails is terrible. This is compounded by the fact

edit/update collection_select with has-many-through association

时光怂恿深爱的人放手 提交于 2019-12-12 00:47:37
问题 I have the following models: RECIPE, TAG and TAGGING (join table) recipe.rb has_many :taggings has_many :tags, through: :taggings accepts_nested_attributes_for :taggings tag.rb has_many :taggings has_many :recipes, through: :taggings scope :diet, -> { where(type_id: 1).to_a } scope :category, -> { where(type_id: 2).to_a } scope :festivity, -> { where(type_id: 3).to_a } scope :daily, -> { where(type_id: 4).to_a } Everything normal so far. But, in my TAGS table I have a field called "type_id"

access has_many relation in two different ways ActiveRecord Rails

烂漫一生 提交于 2019-12-11 20:26:05
问题 I need to access institution in two ways. My models are given below: class Person < ActiveRecord::Base has_many :institution_people has_many :institution_choices has_many :institutions, :through => :institution_people has_many :institutions, :through => :institution_choices fields........ end class Institution < ActiveRecord::Base has_many :people, :through => :institution_people has_many :people, :through => :institution_choices has_many :institution_people has_many :institution_choices end

Has Many through with simpleform

左心房为你撑大大i 提交于 2019-12-11 20:10:52
问题 I'm struggling to get going with a simpleform field for a hasmany through association. When i submit the form the has_many records are not saved, and if the form doesn't validate the options are not selected when the form redisplays. so in short, nothing is working. I'm not sure how much automagic should be going on here, or if I am just missing code i need to make it work. I don't think it's a strong parameters issue as I am not getting a permit params error in the logs. model class Coupon <

How do I access the data from a link in an association?

﹥>﹥吖頭↗ 提交于 2019-12-11 14:49:21
问题 I have: Aulas has_many :grades has_many :students, :through => :grades Students has_many :grades has_many :aulas, :through => :grades Grades belongs_to :aula belongs_to :student I want to show grades.name from the conection between that Aula and a specific Student inside the Aula. This doesn't work, but you will understand what I want: <% aula.students.each do |student| %> <%= link_to student.name, student %>-<%= student.grade.name %> <% end %> Ok so, i'm inside Aula (id:68) if I do students

Making ActiveRecord join model attributes available in query results

别说谁变了你拦得住时间么 提交于 2019-12-11 12:49:30
问题 Given User and Book models, I've created a join model, ViewedBook, that contains additional attributes. Below is the essence of what I've come up with: create_table "users" t.string "username" end create_table "books" t.string "title" t.integer "user_id" t.date "authored_date" end create_table "books_viewings" t.integer "book_id" t.integer "user_id" t.boolean "finished" t.date "last_viewed_date" end class User belongs_to :book_viewing has_many :authored_books, :class_name => "Book", :source =

Trouble with rails naming conventions

青春壹個敷衍的年華 提交于 2019-12-11 10:47:30
问题 I think I have followed the rails naming conventions in my app. But when I am in the terminal testing code I am coming across some errors that go against naming conventions. Here is my terminal session: irb(main):010:0> a = _ => #<Neighborhood id: 24, name: "Lincoln Park", created_at: "2011-12-03 20:29:00", updated_at: "2011-12-03 21:08:47", minlat: 41.91092, maxlat: 41.925658, minlng: -87.648761, maxlng: -87.636117> irb(main):011:0> a.cta_trains NoMethodError: undefined method `cta_trains'

NameError: uninitialized constant for has_many through relationship

和自甴很熟 提交于 2019-12-11 09:42:20
问题 I can't seem to get this right. My has many through relationship just isn't working. Here's the setup: class Group < ActiveRecord::Base belongs_to :user has_many :groups_phone_numbers, :dependent => :destroy has_many :phone_numbers, through: :groups_phone_numbers attr_accessible :name end class PhoneNumber < ActiveRecord::Base belongs_to :user has_many :responses has_many :groups_phone_numbers has_many :groups, through: :groups_phone_numbers attr_accessible :label, :number end class