I'm following along with this tutorial to create Twitter like following in my app, however, my following isn't reciprocal, in fact, the other side doesn't follow a user at all. My two main models are User and Stock. A user should be able to 'follow' a stock, but a stock does not ever follow a user.
models/user.rb
class User < ActiveRecord::Base
has_many :stock_relationships
has_many :stocks, through: :stock_relationships, source: :user
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook, :twitter, :linkedin, :google_oauth2]
# stock following/unfollowing
def follow_stock(stock)
stock_relationships.create(stock_id: stock.id)
end
def unfollow_stock(stock)
stock_relationships.find_by(stock_id: stock.id).destroy
end
def following_stock?(stock)
stock_relationships.include?(stock.id)
end
end
Since my Stock model doesn't really belong to anyone I don't have anything in that model yet:
models/stock.rb
class Stock < ActiveRecord::Base
end
To keep track of the users that are following stocks, I've created another model called StockRelationships:
models/stock_relationship.rb
class StockRelationship < ActiveRecord::Base
belongs_to :user
end
I'm starting off in console by assigning a user:
user = User.find(1)
I'm having trouble because this doesn't seem to be working when using the following_stock? method.
I can create a stock_relationship:
pry(main)> user.follow_stock(stock)
(0.2ms) BEGIN
SQL (2.2ms) INSERT INTO "stock_relationships" ("stock_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["stock_id", 2], ["user_id", 1], ["created_at", "2016-07-12 01:01:00.552580"], ["updated_at", "2016-07-12 01:01:00.552580"]]
(2.5ms) COMMIT
=> #<StockRelationship:0x007ff0b960ba60
id: 3,
user_id: 1,
stock_id: 2,
created_at: Tue, 12 Jul 2016 01:01:00 UTC +00:00,
updated_at: Tue, 12 Jul 2016 01:01:00 UTC +00:00>
and I can unfollow a stock:
[10] pry(main)> user.unfollow_stock(stock)
StockRelationship Load (0.4ms) SELECT "stock_relationships".* FROM "stock_relationships" WHERE "stock_relationships"."user_id" = $1 AND "stock_relationships"."stock_id" = $2 LIMIT 1 [["user_id", 1], ["stock_id", 2]]
(0.1ms) BEGIN
SQL (0.3ms) DELETE FROM "stock_relationships" WHERE "stock_relationships"."id" = $1 [["id", 3]]
(1.5ms) COMMIT
=> #<StockRelationship:0x007ff0b9d36a60
id: 3,
user_id: 1,
stock_id: 2,
created_at: Tue, 12 Jul 2016 01:01:00 UTC +00:00,
updated_at: Tue, 12 Jul 2016 01:01:00 UTC +00:00>
However, I'm running into problems when I'm checking to see if a user is actually following a stock:
[13] pry(main)> user.following_stock?(stock)
=> false
Which should be returning true since the user is in fact following the stock:
[15] pry(main)> StockRelationship.all
StockRelationship Load (0.4ms) SELECT "stock_relationships".* FROM "stock_relationships"
=> [#<StockRelationship:0x007ff0bfa00408
id: 2,
user_id: 1,
stock_id: 1,
created_at: Tue, 12 Jul 2016 00:32:55 UTC +00:00,
updated_at: Tue, 12 Jul 2016 00:32:55 UTC +00:00>,
#<StockRelationship:0x007ff0bfa002c8
id: 4,
user_id: 1,
stock_id: 2,
created_at: Tue, 12 Jul 2016 01:04:06 UTC +00:00,
updated_at: Tue, 12 Jul 2016 01:04:06 UTC +00:00>]
Am I implementing include? incorrectly? Have I messed up something in my models?
Thanks in advance!
Your error is a datatype comparison problem. Try this:
def following_stock?(stock)
# either use this
stock_relationships.where(stock_id: stock.id).present?
# OR
stock_relationships.pluck(:stock_id).include?(stock.id)
end
I am not at a computer in which i can test at this very moment, however looking at your code, I get a sneaky suspicion the cause may be the source declaration in your has_many through relation.
In your User model you have source: :user which i believe is making the relation look for the user_id and not the stock_id when searching from the User side of the relation.
You may want to change it to source: :stock, or in your case as the model name and relation are the same, it might be okay for you to leave out the source decleration completely.
Related
I have a table with 2 foreign key relationships - to a Timeline and a Phase. When I create a record in my development database it all works 100% as expected, but when I do it in test mode it refuses to add the Timeline - you can see from the INSERT statement that it flatly refuses .. it doesn't even try to add it. When I run the exact same sequence below in development it's fine
I can add/update timeline_id but then it doesn't reference the timeline through the parent phase_timeline object as it should. I repeat that this all works fine in development, but not in test. Its driving me mad. Is it failing a validation possibly.. or could the database be corrupt. Are there some console commands I could run to check out the foreign key relationship further?
[33] pry(main)> t = Timeline.last
Timeline Load (0.3ms) SELECT "timelines".* FROM "timelines" ORDER BY "timelines"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Timeline:0x0055fd716dcfa8 id: 1, title: "England", timelineable_type: "Global", timelineable_id: 1, created_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00, updated_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00>
[34] pry(main)> p = Phase.last
Phase Load (1.3ms) SELECT "phases".* FROM "phases" WHERE "phases"."deleted_at" IS NULL ORDER BY "phases"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Phase:0x0055fd717f8450
id: 1,
name: "First phase",
development_id: 1,
created_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00,
updated_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00,
developer_id: 1,
division_id: 1,
number: 1,
deleted_at: nil,
total_snags: 0,
unresolved_snags: 0,
business: "core">
[35] pry(main)> pt = PhaseTimeline.create(phase: p, timeline: t)
(0.2ms) BEGIN
SQL (0.5ms) **INSERT INTO "phase_timelines" ("phase_id") VALUES ($1) RETURNING "id" [["phase_id", 1]]**
(1.8ms) COMMIT
=> #<PhaseTimeline:0x0055fd719ef9c0 id: 5, phase_id: 1, timeline_id: nil>
After a LOT of head scratching and diving into the bowels .. this problem was caused by having 2 model classes with the same name. The classes were in 2 separate folder but had the same scope and were identical but removing the errant one sorted the problem
I wonder if anyone can help me to understand about the difference between build and create in Has Many Through (HMT) relationship?
From my understanding after reading through stackoverflow and google, create is essentially build + save. However, it seems like create does more than just build + save. It also somehow save into the join table of the HMT relationship as shown below.
I created 3 models: user, wiki, and collaborator.
class User < ActiveRecord::Base
has_many :wikis, through: :collaborators
has_many :collaborators
end
class Wiki < ActiveRecord::Base
has_many :users, through: :collaborators
has_many :collaborators
end
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
end
Then I tested the build and create behavior in rails console:
$rails c
Loading development environment (Rails 4.0.10)
> user = User.first #create instance user
User Load SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "Jayzz55">
> (user.wikis.build(title:"hello",body:"world")).save #build and save wiki
SQL INSERT INTO "wikis" ("body", "created_at", "title", "updated_at") VALUES(?,?,?,?) [["body","world"], ["ccreated_at, Sat, 08 Nov 2014 01:39:36 UTC +00:00], ["title","hello"], ["updated_at",Sat, 08 Nov 2014 01:39:36 UTC +00:00]]
=> true
>wiki1 = Wiki.first #create instance wiki called wiki1
=> #<Wiki id:1, title:"hello",body:"world">
>user.wikis
=> #<Wiki id:1, title:"hello",body:"world">
>user.wikis.count
=> 0
>wiki1.users
=> []
>Collaborator.all
=> []
> user.wikis.create(title:"second title",body:"another one")
begin transaction
SQL INSERT INTO "wikis" ("body", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?)[0m [["body", "another one"], ["created_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00], ["title", "second title"], ["updated_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00]]
SQL INSERT INTO "collaborators" ("created_at", "updated_at", "user_id", "wiki_id") VALUES (?, ?, ?, ?) [["created_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00], ["updated_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00], ["user_id", 1], ["wiki_id", 2]]
=> #<Wiki id:2, title:"second title",body:"another one>
>user.wikis
=> [#<Wiki id: 1, title:"hello",body:"world">, #<Wiki id:2, title:"second title",body:"another one>]
>user.wikis.count
=> 1
>wiki2.users
=>#<User id: 1, name: "Jayzz55">
>Collaborator.all
Collaborator Load SELECT "collaborators".* FROM "collaborators"
=> #<Collaborator id:`, user_id:1, wiki_id: 2>
Case wiki1 : build and then save it
The data is not saved into the join table. calling user.wikis does return the object wiki, but running user.wikis.count return 0. Furthermore, running wiki1.users doesn't return the object user. Checking the join table Collaborator returns empty array.
Case wiki2 : create
The data is saved into the join table. calling user.wikis does return the object wiki. Running user.wikis.count return 1. Furthermore, running wiki1.users does return the object user. Checking the join table Collaborator, it does show the relationship clearly mapped.
Seems like Create is not simply just build + new. I'm curious about this behavior and hopefully someone can share their knowledge on this.
I believe that if you had in your first case instead written:
user.wikis.build(title:"hello",body:"world")
user.save
... you would find that ActiveRecord does the "full" save that create also does. The way you've written it, you're asking ActiveRecord to save the newly created Wiki instance, but not the association. So it doesn't create the record in the join table that's required.
Build + Save is equivalent to Create in any relationship
I have a application that allows a user to upload a creative and assign it to multiple weeks
Week Model
class Week < ActiveRecord::Base
has_many :creative_weeks
has_many :creatives, :through => :creative_weeks
end
Creative Model
class Creative < ActiveRecord::Base
has_many :creative_weeks
has_many :weeks, :through => :creative_weeks
mount_uploader :image, CreativeUploader
end
Creative Weeks [Join Table]
class CreativeWeek < ActiveRecord::Base
belongs_to :week
belongs_to :creative
end
I know the association works which allows me to issue a creative to multiple weeks based on my console:
2.0.0p353 :020 > c = Creative.first
Creative Load (0.2ms) SELECT "creatives".* FROM "creatives" ORDER BY "creatives"."id" ASC LIMIT 1
=> #<Creative id: 10, name: "", account_id: 1, week_id: nil, campaign_id: 1, image: "Quakes_2013_DigiOOH_40YR_704x496.jpg", created_at: "2014-02-20 18:13:47", u
pdated_at: "2014-02-20 18:13:47">
2.0.0p353 :021 > c.week_ids
(0.2ms) SELECT "weeks".id FROM "weeks" INNER JOIN "creative_weeks" ON "weeks"."id" = "creative_weeks"."week_id" WHERE "creative_weeks"."creative_id" = ? [["
creative_id", 10]]
=> [3]
2.0.0p353 :022 > c.week_ids = [1, 2, 3]
Week Load (0.2ms) SELECT "weeks".* FROM "weeks" WHERE "weeks"."id" IN (1, 2, 3)
Week Load (0.1ms) SELECT "weeks".* FROM "weeks" INNER JOIN "creative_weeks" ON "weeks"."id" = "creative_weeks"."week_id" WHERE "creative_weeks"."creative_id"
= ? [["creative_id", 10]]
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "creative_weeks" ("created_at", "creative_id", "updated_at", "week_id") VALUES (?, ?, ?, ?) [["created_at", Thu, 20 Feb 2014 18:20:27
UTC +00:00], ["creative_id", 10], ["updated_at", Thu, 20 Feb 2014 18:20:27 UTC +00:00], ["week_id", 1]]
SQL (0.1ms) INSERT INTO "creative_weeks" ("created_at", "creative_id", "updated_at", "week_id") VALUES (?, ?, ?, ?) [["created_at", Thu, 20 Feb 2014 18:20:27
UTC +00:00], ["creative_id", 10], ["updated_at", Thu, 20 Feb 2014 18:20:27 UTC +00:00], ["week_id", 2]]
(1.0ms) commit transaction
=> [1, 2, 3]
2.0.0p353 :023 >
The issue I am having is getting this same functionality to work on the front end. It will only pass in one value, typically the last one chosen
In my form:
<div class="field">
<%= f.collection_select(:week_ids, Week.all, :id, :start_at, {}, multiple: true, name: 'creative[week_ids]') %>
</div>
Can anyone advise me what I am missing?
TIA
Change the select tag's name to this:
name: 'creative[week_ids][]'
That extra '[]' at the end of the name specifies that you want an array of values to be posted.
Using strong parameters you have to specify that the value will be an array:
def your_strong_params
params.require(:creative).permit(week_ids: [])
end
This question already has an answer here:
Adding a JOIN between two tables
(1 answer)
Closed 9 years ago.
My Organization class looks something like this:
has_many Students
My Student class looks like this:
has_many Klasses
belongs_to Organization
My Klass class looks like this:
some field named : price
scope :top_expensive_classes, joins(:students).order('price DESC')
belongs_to Student
And my query looks like this:
#results = Klass.top_expensive_classes.where(organization_id: params[:id]).limit(RESULT_SET_COUNT)
Notice that it starts with Klass, so that's the problem because I am searching in the where class for organization_id but that is not in the Klass, it is in Student class , so somehow I should introduce a join somewhere to fix this but couldn't figure it out.
I think the real issue is your associations are likely incorrect.
A student has many classes
A class has many students
but what you have is
A student has many classes
A class belongs to a single student
This doesn't really make sense (at least in any situation I've ever seen a class and student interact). You should be creating a many-to-many relationship instead of a one-to-many relationship between Klass and Student.
class Student < ActiveRecord::Base
has_many :klasses, through: :student_klasses
has_many :student_klasses
end
class Klass < ActiveRecord::Base
has_many :students, through: :student_klasses
has_many :student_klasses
end
class StudentKlass < ActiveRecord::Base
belongs_to :student
belongs_to :klass
end
Once you have these correct associations in place, you need to call .joins on the :students association from the Klass class. You can do without the scope.
Klass.joins(:students).where("students.organization_id = ?", params[:id]).order('price DESC').limit(RESULT_SET_COUNT)
Read the guide on ActiveRecord Querying.
Here is the proof (using the exact model definitions above) that the ordering of the associations does not matter.
irb(main):001:0> s = Student.create(name: "Deefour")
SQL (3.6ms) INSERT INTO "students" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:33:32 UTC +00:00], ["name", "Deefour"], ["updated_at", Fri, 08 Mar 2013 01:33:32 UTC +00:00]]
=> #<Student id: 1, name: "Deefour", created_at: "2013-03-08 01:33:32", updated_at: "2013-03-08 01:33:32">
irb(main):002:0> kk = []
=> []
irb(main):003:0> kk << Klass.create(title: "Klass 1")
SQL (0.3ms) INSERT INTO "klasses" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:06 UTC +00:00], ["title", "Klass 1"], ["updated_at", Fri, 08 Mar 2013 01:34:06 UTC +00:00]]
=> [#<Klass id: 1, title: "Klass 1", created_at: "2013-03-08 01:34:06", updated_at: "2013-03-08 01:34:06">]
irb(main):004:0> kk << Klass.create(title: "Klass 2")
SQL (0.3ms) INSERT INTO "klasses" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:14 UTC +00:00], ["title", "Klass 2"], ["updated_at", Fri, 08 Mar 2013 01:34:14 UTC +00:00]]
=> [#<Klass id: 1, title: "Klass 1", created_at: "2013-03-08 01:34:06", updated_at: "2013-03-08 01:34:06">, #<Klass id: 2, title: "Klass 2", created_at: "2013-03-08 01:34:14", updated_at: "2013-03-08 01:34:14">]
irb(main):005:0> s.klasses = kk
Klass Load (0.1ms) SELECT "klasses".* FROM "klasses" INNER JOIN "student_klasses" ON "klasses"."id" = "student_klasses"."klass_id" WHERE "student_klasses"."student_id" = ? [["student_id", 1]]
SQL (0.4ms) INSERT INTO "student_klasses" ("created_at", "klass_id", "student_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00], ["klass_id", 1], ["student_id", 1], ["updated_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00]]
SQL (0.1ms) INSERT INTO "student_klasses" ("created_at", "klass_id", "student_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00], ["klass_id", 2], ["student_id", 1], ["updated_at", Fri, 08 Mar 2013 01:34:29 UTC +00:00]]
=> [#<Klass id: 1, title: "Klass 1", created_at: "2013-03-08 01:34:06", updated_at: "2013-03-08 01:34:06">, #<Klass id: 2, title: "Klass 2", created_at: "2013-03-08 01:34:14", updated_at: "2013-03-08 01:34:14">]
irb(main):006:0> Student.first.klasses.map(&:id)
Student Load (0.2ms) SELECT "students".* FROM "students" ORDER BY "students"."id" ASC LIMIT 1
Klass Load (0.1ms) SELECT "klasses".* FROM "klasses" INNER JOIN "student_klasses" ON "klasses"."id" = "student_klasses"."klass_id" WHERE "student_klasses"."student_id" = ? [["student_id", 1]]
=> [1, 2]
I have a model,group_question_answer.rb
class GroupQuestionAnswer < ActiveRecord::Base
belongs_to :group_question
validates_presence_of :answer
validates_presence_of :answer_question
end
for attribute answer and answer_question i get error message as Group question answers answer can't be blank
I need to show only answer cant be blank.i even tried adding :message=>"cant be blank",but still i dont get my required message.how can i remove model name and can just arrtibute error message ....
class GroupQuestionAnswer < ActiveRecord::Base
attr_accessible :answer
validate do |group_question_answer|
errors.add(:base, "answer can't be blank") if group_question_answer.answer.blank?
end
end
works perfectly
rails c
Loading development environment (Rails 3.2.9)
irb(main):001:0> q = GroupQuestionAnswer.create
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> #<GroupQuestionAnswer id: nil, answer: nil, created_at: nil, updated_at: nil>
irb(main):002:0> q
=> #<GroupQuestionAnswer id: nil, answer: nil, created_at: nil, updated_at: nil>
irb(main):003:0> q.save
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> false
irb(main):004:0> q.errors
=> #<ActiveModel::Errors:0x007fc2fb325fa8 #base=#<GroupQuestionAnswer id: nil, answer: nil, created_at: nil, updated_at: nil>, #messages={:base=>["answer can't be blank"]}>
irb(main):006:0> q.errors.messages
=> {:base=>["answer can't be blank"]}
=> {:base=>["answer can't be blank"]}
irb(main):007:0> q = GroupQuestionAnswer.create(answer: "123")
(0.1ms) begin transaction
SQL (9.0ms) INSERT INTO "group_question_answers" ("answer", "created_at", "updated_at") VALUES (?, ?, ?) [["answer", "123"], ["created_at", Fri, 28 Dec 2012 11:01:38 UTC +00:00], ["updated_at", Fri, 28 Dec 2012 11:01:38 UTC +00:00]]
(1.1ms) commit transaction
=> #<GroupQuestionAnswer id: 1, answer: "123", created_at: "2012-12-28 11:01:38", updated_at: "2012-12-28 11:01:38">
irb(main):008:0> q.errors.messages
=> {}
In my opinion validate method perfect way to fully customize rails validations and that does exactly what you ask for.
You can try like this:
validates :answer, presence: { message: '<Your message>'}
validates :answer_question, presence: { message: '<Your message>'}
Try adding them in your config/locales/en.yml file
As there you can do something like this,
en:
errors:
messages:
answer: answer can't be blank