Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Cottage
20,718 PointsActive Record Joined Table Query Problem. Please Help!
Hello Everyone!
I've been struggling with this problem for the last few hours; any help would be greatly appreciated.
I have three models Attorney, CompletedRecord, and User
Attorney:
class Attorney < ActiveRecord::Base
has_many :counties, dependent: :destroy
has_many :completed_records, dependent: :destroy
validates :attorney_name, uniqueness: true
end
CompletedRecord:
class CompletedRecord < ActiveRecord::Base
belongs_to :attorney
belongs_to :user
validates :stage, presence: true
validates :completed_by, presence: true
validates :completed_day, presence: true
end
User:
class User < ActiveRecord::Base
# :confirmable, :lockable, :timeoutable and :omniauthable
has_many :completed_records
:lockable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
In my controller I need to check 2 things in the same if statement, and assign the a variable to the @attorneys instance variable:
Find the first Attorney where there are no associated "completed_record" records, and assign them to the @attorney instance variable, or
If all attorneys have "completed_record" records then find the first attorney that does not have any attorney.completed_records with the user_id of current_user.id, and assign that to the @attorney instance variable
I have tried something like this:
if @attorney = Attorney.where(stage: 1, locked: false).includes(:completed_records).where(:completed_records => {user_id: nil}).first || Attorney.where(stage: 1, locked: false).includes(:completed_records).where.not(:completed_records => { user_id => current_user.id}).first
I have also tried the following:
def stage1
attorney = Attorney.where(stage: 1, locked: false, criminal: nil)
if @attorney = attorney.includes(:completed_records).where(completed_records: {user_id: nil}).first
@attorney.update_attributes(locked: true)
render 'stage1'
elsif @attorney = attorney.joins(:completed_records).where.not(completed_records: {user_id: current_user.id}).first
@attorney.update_attributes(locked: true)
render 'stage1'
else
redirect_to root_path
end
end
the if statements are acting as I need in the rails console, but they don't seem to work when in the controller. For instance:
Attorney.where(stage: 1, locked: false, criminal: nil).joins(:completed_records).where.not(completed_records: {user_id: 1}).first
returns records that have not been completed by the user with user id = 1, but it seems to always return nil when it's in the controller.
Thanks in advance for the help!