I would like to select in simple_form <%= f.association :organisators, collection: User.all %> and <%= f.association :helpers, collection: User.all %> that source of them will be User ids stored in Participation join table with appropriate enum kind, is it a way to do it automatically with ActiveRecord relations?
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :event
enum kind: [:helper, :organisator]
end
class Event < ActiveRecord::Base
belongs_to :user
has_many :participations
has_many :organisators, class_name: 'Participation'
has_many :helpers, class_name: 'Participation'
end
class User < ActiveRecord::Base
has_many :events
has_many :participations
end
When I try to save current version then it raise: Couldn't find Participation with 'id'=1
event_params: {"helper_ids"=>["2"], "organisator_ids"=>["1"]}
I would recommend using Single Table Inheritance over an enum. Keep the existing Participation model, and create two other models (helper and organiser) which will inherit from it.
Once you have implemented this, you can have two different object types which can be referenced through relationships while sharing one database model.
Take a look at this documentation:
http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html
source of them will be User ids stored in Participation join table
You need to pull from the Participation model, not the User model:
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :event
enum kind: [:helper, :organisator]
end
class Event < ActiveRecord::Base
belongs_to :user
has_many :participations
has_many :organisators, -> { where kind: :organisator}, class_name: 'User', through: :participations, source: :user
has_many :helpers, -> { where kind: :helper}, class_name: "User", through: :participations, source: :user
end
class User < ActiveRecord::Base
has_many :events
has_many :participations
has_many :participated_events, through: :participations
end
You've basically got a has_many :through relationship.
--
Thus, you should be able to use:
#app/views/events/update.html.erb
<%= simple_form_for #event do |f| %>
<%= f.association :organisators, collection: #event.organisators %>
<%= f.association :helpers, collection: #event.helpers %>
<%= f,submit %>
<% end %>
It would help if you gave some context on what you're trying to achieve. The above code should help, we may have to tweak it a little.
Related
Sorry about the title, couldn't come up with a better one to describe what I'm trying to do.
In a previous question, some users suggested I could simplify the models. I didn't get anymore comments but I think I'm doing it 'the right way' because I need to store additional attributes in the join tables.
Anyway my models are setup like this.
class Student < ActiveRecord::Base
has_many :student_notes
has_many :notes, :through => :student_notes
has_many :relationships
has_many :users, :through => :relationships
end
class Note < ActiveRecord::Base
has_many :student_notes
has_many :students, :through => :student_notes
end
class StudentNote < ActiveRecord::Base
belongs_to :student
belongs_to :note
end
class User < ActiveRecord::Base
has_many :relationships
has_many :students, :through => :relationships
has_many :notes, :through => :students
end
class Relationship < ActiveRecord::Base
belongs_to :student
belongs_to :user
end
Now, in my note show view I have this:
...
<% #note.students.each do |student| %>
<tr>
<td><%= link_to student.full_name, student %></td>
</tr>
<% end %>
...
This works, except I would like to show only the students that belong to current_user.
Any tips on how can I achieve it? I guess a helper method in the model would be the way to go, but I'm totally lost.
Thank you!
On Student:
scope :belonging_to, -> (u) { joins(:users).where users: { id: u.id } }
Then:
#note.students.belonging_to(current_user)...
in my app users has_many categories categories have sub_categories in db i create parent_id with main category id
and now i dont know how show main category if user select only sub_category
User.rb
has_many :users_ecategories
has_many :ecategories, through: :users_ecategories
Category.rb
class Ecategory < ActiveRecord::Base
has_many :users_ecategories
has_many :users, through: :users_ecategories
has_many :ecategories, class_name: 'Ecategory', foreign_key: 'parent_id'
end
Users_categories.rb
class UsersCategory < ActiveRecord::Base
belongs_to :user
belongs_to :ecategory
end
views/user/show.html.erb
<% #user.ecategories.each do |ecategory| %>
<%= ecategory.name %>
<%= ecategory.id %>
<% end %>
how to show main category name? <%= ecategory.parent_id.name %> doesn't work
please help
I think you need to define the parent relation
class Ecategory < ActiveRecord::Base
has_many :users_ecategories
has_many :users, through: :users_ecategories
has_many :ecategories, class_name: 'Ecategory', foreign_key: 'parent_id'
belongs_to :parent_ecategory, class_name: 'Ecategory', foreign_key: 'parent_id'
end
Then the call would be
<%= ecategory.parent_ecategory.name %>
I have 3 tables. pin, genre and genres_pins.
genres_pins joins the pin and genre tables together with a many to many. Here's my setup:
Pin Model
class Pin < ActiveRecord::Base
belongs_to :user
belongs_to :type
has_many :replies
has_many :genres_pins
has_many :genres, :through => :genres_pins
end
Genre Model
class Genre < ActiveRecord::Base
has_many :genres_pins
has_many :pins, :through => :genres_pins
end
GenresPins Model
class GenresPins < ActiveRecord::Base
belongs_to :pin
belongs_to :genre
end
View
<% pin.genres_pins.each do |g| %>
<%= g.title %>
<% end %>
I get the following error:
uninitialized constant Pin::GenresPin
Any idea what's going on here? I'm new to Rails, so may be missing something stupidly obvious.
Help appreciated.
Many thanks,
Michael.
class GenrePin < ActiveRecord::Base
belongs_to :pin
belongs_to :genre
end
The name of the class should be changed
My application consists of a drink model
class Drink < ActiveRecord::Base
attr_accessible :name
has_many :recipe_steps, :dependent => :destroy
has_many :ingredients, through: :recipe_steps
end
An ingredient model
class Ingredient < ActiveRecord::Base
attr_accessible :name
has_many :recipe_steps
end
how would I go about having it so when a user searches an ingredient that it returns all of the drinks with that ingredient?
Additional information: I'm currently using sunspot/solr for my searching.
First, in your Ingredient model you'd need this line:
has_many :drinks, through: :recipe_steps
To define the has_many, through: relationship. Make sure that RecipeStep has these lines, too:
belongs_to :ingredient
belongs_to :drink
Then you can do something like in the DrinksController:
def search
term = params[:search]
ingredient = Ingredient.where(:name => term)
#drinks = Ingredient.find(ingredient).drinks
end
And your form should look something like this:
<%= form_for #drink, :url => { :action => "search" } do |f| %>
<%= f.text_field :search %>
<% end %>
I don't know all your names for everything but this should get you going.
Following should work fine:
class Ingredient < ActiveRecord::Base
...
has_many :recipe_steps
has_many :drinks, through: :recipe_steps
end
I have such terrible models:
class ParentalRelation < ActiveRecord::Base
belongs_to :parent
belongs_to :student
belongs_to :counselor
belongs_to :parental_relation_type
end
class ParentalRelationType < ActiveRecord::Base
has_many :parental_relations
end
class Parent < ActiveRecord::Base
has_many :parental_relations
has_many :students, :through => :parental_relations
has_many :counselors, :through=> :parental_relations
has_many :parental_relation_types, :through=> :parental_relations
belongs_to :user, :dependent=> :destroy
belongs_to :occupation_type
accepts_nested_attributes_for :user
end
Parental relation types are like father, mother, etc. The reasoning is that a parental relation between one counselor, one parent and one student is unique and counselors should not see the relations that belong other counselors.
In controllers/parent_controller/edit action I have:
#parental_relation= ParentalRelation.find_by_counselor_id_and_student_id_and_parent_id(x, y, z)
In views/parent/_form.html.erb I have:
<%= form_for #parent do |f| %>
inside that form I need a collection_select for ParentalRelationType.all and select the parent's parental_relation_type_id for that particular parental relation, but I can't find the syntax to do it.
I tried adding
<%= collection_select(#parental_relation, :parental_relation_type_id, ParentalRelationType.all, :id, :name) %>
underneath the form_for, but the relation type id is 2, and default 1 is selected instead.
Added this to parents/_form
<%= fields_for #counselor_student_parent do |csp| %>
<%= f.label :parental_relation_type_id %>
<%= collection_select(:student_counselor_parent, :parental_relation_type_id, ParentalRelationType.all, :id, :name) %>
<% end %>
And this to parents_controller/new
def new
#counselor= Counselor.find(params[:counselor_id])
#student= Student.find(params[:student_id])
#parent= #student.parents.build
#parent_user= #parent.build_user
#counselor_student_parent= #counselor.student_counselor_parents.build
end