New to Ruby, Rails, and OOP in general. I've been through the "Ruby on Rails Tutorial: Learn Web Development with Rails" and about 80% through "Agile Web Development with Rails 4" but I can't visually figure this out. I was hoping someone can help me understand, maybe I'm wording it wrong.
Models: User, Team, Membership, Order, OrderLine
It's a pretty basic setup. (short hand, if you will...) Team belongs_to :user (owner, not terribly relevant here) Users has_many :teams through: :memberships Team has_many :users through: :memberships OrderLine belongs_to :team Note an OrderLine is assigned to a team, not a whole Order.
I'm trying to display all OrderLines which are associated with the currently logged in user (current_user). While this doesn't actually work, I feel it's close to producing. It also feels super dirty.
def index
@memberships = current_user.memberships.ids
membership_list = @memberships.join(", ")
@OrderLines = OrderLine.where("team_id IN (?)", membership_list)
end
Cheers!
# Model
class User < ActiveRecord::Base
has_many :owned_team, :class_name => "Team"
has_many :teams
has_many :memberships
has_many :order_lines, through: :memberships
class Team < ActiveRecord::Base
belongs_to :faction
belongs_to :region
belongs_to :owner, :class_name => "User"
has_many :users, through: :memberships
has_many :order_lines
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :team
has_many :order_lines, through: :team
# Controller
@OrderLines = current_user.order_lines
Some times all you need is a break.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.