I have some users who take quizzes. I track the result they chose. I need to figure out how to allow them to change their quiz submission. If I just associate the answer, they'll have answered the question twice. Building up the data is complex, does ActiveRecord provide a way to deal with this?
This whole example will run in a standalone file.
Here is my schema:
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table :users do |t|
t.string :name
end
create_table :questions do |t|
t.string :name
end
create_table :question_results do |t|
t.string :name
t.integer :question_id
end
create_table :question_results_users do |t|
t.integer :user_id
t.integer :question_result_id
end
end
here are my models:
class User < ActiveRecord::Base
has_and_belongs_to_many :question_results
has_many :questions, through: :question_results
end
class Question < ActiveRecord::Base
has_many :question_results
end
class QuestionResult < ActiveRecord::Base
belongs_to :question
has_and_belongs_to_many :users
end
Lets make three questions and some answers:
Question.create! name: "What's your favourite movie?" do |question|
question.question_results.build name: 'Gattaca'
question.question_results.build name: 'Super Troopers'
end
Question.create! name: 'Who do you want to be president?' do |question|
question.question_results.build name: 'Barack Obama'
question.question_results.build name: 'Mitt Romney'
question.question_results.build name: 'Mickey Mouse'
end
Question.create! name: "What's your favourite colour?" do |question|
question.question_results.build name: 'black'
question.question_results.build name: 'green'
end
Lets make a user:
jim = User.create! name: 'Jim'
jim.question_results << QuestionResult.find_by_name('Gattaca')
jim.question_results << QuestionResult.find_by_name('Barack Obama')
jim.question_results << QuestionResult.find_by_name('black')
jim.question_results # => [#<QuestionResult id: 1, name: "Gattaca", question_id: 1>, #<QuestionResult id: 3, name: "Barack Obama", question_id: 2>, #<QuestionResult id: 6, name: "black", question_id: 3>]
jim.question_results.map(&:question) # => [#<Question id: 1, name: "What's your favourite movie?">, #<Question id: 2, name: "Who do you want to be president?">, #<Question id: 3, name: "What's your favourite colour?">]
Now Jim changes his mind, he decides he likes Super Troopers and doesn't want to vote, but he still likes black. How do I update this without having him answer the question multiple times?
# pretend we're in a controller here (also note that I can change the format of the data, if there is something more convenient)
posted_from_form = {
questions: {
Question.all[0].id => 'Super Troopers',
Question.all[1].id => '',
Question.all[2].id => 'black',
}
}
First lets change the format of our data so that the results are in id form instead of name form:
posted_from_form = {
questions: {
Question.all[0].id => QuestionResult.find_by_name('Super Troopers').id.to_s,
Question.all[1].id => '',
Question.all[2].id => QuestionResult.find_by_name('black').id,
}
}
Then you can set the ids directly, and ActiveRecord will handle all of the complexity:
jim.question_result_ids = posted_from_form[:questions].values # => ["2", "", 6]
jim.question_results # => [#<QuestionResult id: 6, name: "black", question_id: 3>, #<QuestionResult id: 2, name: "Super Troopers", question_id: 1>]
Related
I'm taking a course on Lynda.com (Ruby on Rails 5 Essential Training) and I'm having an issue with adding a record on a table. Here's some details: The objective is to create a joint table, Many-to-Many association, so we're first trying to create a record on with of the tables we want to use on the new table. And everytime I write this line:
section = Sections.create(:name => "Section One", :position => 1)
It gives me this
(0.2ms) BEGIN
(0.3ms) ROLLBACK
=> #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil>
I checked my code and everything seems fine. By the way inserting records on other tables works. It's just this table.
one important point, This table is a previously created table. It's the new one we're trying to create.
What am I doing wrong?
Here is my code from migrate:
class CreateSections < ActiveRecord::Migration[5.2]
def up
create_table :sections do |t|
t.integer "page_id"
t.string "name"
t.integer "position"
t.boolean "visible", :default => false
t.string "content_type"
t.text "content"
t.timestamps
end
add_index("sections", "page_id")
end
def down
drop_table :sections
end
end
Here is the Section model:
class Section < ApplicationRecord
belongs_to :page
has_many :section_edits
end
The error is caused by: belongs_to :page as the page_id is nil and by default Rails belongs_to helper is adding a presence validation to make sure that the association is valid.
To disable this behaviour (presence validation) you can use:
belongs_to :page, optional: true
as mentioned here: https://guides.rubyonrails.org/association_basics.html#options-for-belongs-to
or you can add page_id to your Section.create call as mentioned by others:
page_id = 1 # or Page.first.id or any page id you need
section = Section.create(name: "Section One", position: 1, page_id: page_id)
Your error comes from belongs_to :page
If you try with create!, you should see this error message:
ActiveRecord::RecordInvalid: Validation failed: Page must exist
Just add page_id in your section creation:
page_id = 1 # or Page.first.id or any page id you need
section = Section.create(name: "Section One", position: 1, page_id: page_id)
I have been referencing this rails tutorial to try to create a feed where only articles belonging to users who have accepted my friend requests appear in my feed.
However, i have a different schema and user model from the tutorial (i have an extra step where friend request is pending to be accepted). The below method in my user model does not filter out friendships which have not been accepted, hence articles of unconfirmed friends are appearing my feed, which is not what I want.
user.rb
has_many :friendships
has_many :received_friendships, class_name: "Friendship", foreign_key: "friend_id"
has_many :active_friends, -> { where(friendships: { accepted: true}) }, through: :friendships, source: :friend
has_many :received_friends, -> { where(friendships: { accepted: true}) }, through: :received_friendships, source: :user
has_many :pending_friends, -> { where(friendships: { accepted: false}) }, through: :friendships, source: :friend
has_many :requested_friendships, -> { where(friendships: { accepted: false}) }, through: :received_friendships, source: :user
def feed
friend_ids = "SELECT friend_id FROM friendships
WHERE user_id = :user_id"
Article.where("user_id IN (#{friend_ids})
OR user_id = :user_id", user_id: id)
end
schema.rb
create_table "friendships", force: :cascade do |t|
t.integer "user_id"
t.integer "friend_id"
t.boolean "accepted", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Pages controller
def home
#feed = current_user.feed.all
end
home.html.erb
<%= render #feed %>
for your case I think the key is here WHERE (user_id = :user_id AND accepted = true), I get accepted field from friendships schema.
def feed
friend_ids = "SELECT friend_id FROM friendships
WHERE (user_id = :user_id AND accepted = 't')"
Article.where("user_id IN (#{friend_ids})
OR user_id = :user_id", user_id: id)
end
I have the three following tables in Ruby on Rails 4:
The "Decision" table:
class Decision < ActiveRecord::Base
validates :title, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :colour, presence: true, length: { maximum: 20 }, uniqueness: { case_sensitive: false }
has_many :decision_datafields, dependent: :destroy
has_many :datafields, through: :decision_datafields
def datafields
Datafield.where(id: self.decision_datafields.select("datafield_id"))
end
end
The "DecisionDatafield" table (linking table):
class DecisionDatafield < ActiveRecord::Base
validates :min_score, presence: true
validates_inclusion_of :min_score, :in => 1..10
belongs_to :decision
belongs_to :datafield
end
The "Datafield" table:
class Datafield < ActiveRecord::Base
validates :title, presence: true, length: { maximum: 100 }, uniqueness: { case_sensitive: false }
has_many :decision_datafields, dependent: :destroy
has_many :decisions, through: :decision_datafields
has_many :score_options, dependent: :destroy
def decisions
Decision.where(id: self.decision_datafields.select("decision_id"))
end
end
There is also a score options table, but it isn't necessary for the problem.
Anyway, what I'd like to be able to do is do a query like this:
Decision.first.datafields
... and have ActiveRecord retrieve a list of the first Decision's Datafields, along with the corresponding min_score value from the DecisionDatafields linking table.
Right now, the above query will return something like this:
#<ActiveRecord::Relation [#<Datafield id: 1, title: "DF1", created_at: "2015-03-28 09:59:26", updated_at: "2015-03-28 09:59:26">, #<Datafield id: 2, title: "DF2", created_at: "2015-03-28 09:59:26", updated_at: "2015-03-28 09:59:26">]>
... which is nice, but I want it to look like this:
#<ActiveRecord::Relation [#<Datafield id: 1, title: "DF1", created_at: "2015-03-28 09:59:26", updated_at: "2015-03-28 09:59:26", min_score: 5>, #<Datafield id: 2, title: "DF2", created_at: "2015-03-28 09:59:26", updated_at: "2015-03-28 09:59:26, min_score: 7">]>
The difference is that the min_score from the DecisionDatafield linking table has been joined to the records returned by the query.
Thanks!
This might work (haven't tested it):
class Decision < ActiveRecord::Base
...
def data_fields
Datafield.includes(:decision_datafield).select("datafields.*, decision_datafields.min_score").where(id: self.decision_datafields.select("datafield_id"))
end
end
Call it with Decision.first.data_fields
It seems that the following works:
Datafield.where(id: self.decision_datafields.select("datafield_id")).includes(:decision_datafields).joins("left join decision_datafields on datafields.id = decision_datafields.datafield_id").select("datafields.*, decision_datafields.min_score as min_score")
It would appear, however, that when using the Rails console, the min_score does not get added to the record.
However, calling Decision.first.datafields.first.min_score does return a value (it's just not shown when viewing the record via Decision.first.datafields.first.
I have the following setup:
Schema.rb
ActiveRecord::Schema.define(version: 20130923235150) do
create_table "addresses", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "user_addresses", force: true do |t|
t.integer "user_id"
t.integer "address_id"
t.string "purpose"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
end
User.rb:
class User < ActiveRecord::Base
has_one :user_address
has_one :primary_shipping_address, through: :user_address, class_name: :UserAddress, source: :address
has_one :primary_billing_address, through: :user_address, class_name: :UserAddress, source: :address
end
Address.rb:
class Address < ActiveRecord::Base
has_one :user_address
has_one :primary_shipping_user, through: :user_address, class_name: :UserAddress, source: :user
has_one :primary_billing_user, through: :user_address, class_name: :UserAddress, source: :user
end
UserAddress.rb:
class UserAddress < ActiveRecord::Base
belongs_to :user
belongs_to :address
end
When someone does user.primary_billing_address = address, I want the join model instance to have "billing" set as its purpose. Similarly with shipping and "shipping". Ex.
irb(main):013:0> u = User.new
=> #<User id: nil, created_at: nil, updated_at: nil>
irb(main):014:0> a = Address.create
=> #<Address id: 3, created_at: "2013-09-24 00:13:07", updated_at: "2013-09-24 00:13:07">
irb(main):015:0> u.primary_billing_address = a
=> #<Address id: 3, created_at: "2013-09-24 00:13:07", updated_at: "2013-09-24 00:13:07">
irb(main):016:0> u.save!
=> true
irb(main):017:0> u.user_address
=> #<UserAddress id: 2, user_id: 3, address_id: 3, purpose: nil, created_at: "2013-09-24 00:13:18", updated_at: "2013-09-24 00:13:18">
(not what I want... purpose should be "billing")
How can I do this such that it works for new AND persisted records?. I've come up with solutions that are 90% there, but break on some random spec due to an edge case my approach didn't catch.
The trickiest part to work around is how association= behaves: on new records, it queues the association for assignment through the join model.
PS: I left out the conditionals on the has_one relationships that I'd use to get the address I want. I think this issue is independent of that.
First, the associations are a bit off, both primary_shipping_address and primary_billing_address will return same address. You can change it to
class User < ActiveRecord::Base
has_many :user_addresses # user can have multiple addresses one for shipping and one for billing
has_one :primary_shipping_address,
through: :user_address, class_name: :UserAddress,
source: :address, :conditions => ['user_addresses.purpose = ?','shipping']
has_one :primary_billing_address,
through: :user_address, class_name: :UserAddress,
source: :address, :conditions => ['user_addresses.purpose = ?','billing']
end
To save the purpose while saving the address, there are two options.
Option 1 : Override the default association= method
# alias is needed to refer to original method
alias_method :orig_primary_billing_address=, :primary_billing_address=
def primary_billing_address=(obj)
self.orig_primary_billing_address = obj
self.user_addresses.where("address_id = ?", obj.id).update_attribute(:purpose, 'billing')
end
# repeat for shipping
Option 2 : Create a custom method (I prefer this as it is cleaner and DRY)
def save_address_with_purpose(obj,purpose)
self.send("primary_#{purpose}_address=", obj)
self.user_addresses.where("address_id = ?", obj.id).update_attribute(:purpose, purpose)
end
How would you model the references and citations to publications (articles, books, chapters, etc...)?
A publication can be an article, book or a chapter and it has many references to other publications and other publications refer to it (call these citations)
I need to be able to list the relationships among the publications: The references in a publication and the citations from other publications to this publication
My initial understanding is that this would be a polymorphic relationship to handle the different types of publications and that it would require a bidirectionalself join.
My stab at it
Publication
belongs_to :writing, :polymorphic =>true
has_and_belongs_to_many :references
:class_name => "Publication"
:join_table => 'reference_citation'
:foreign_key => 'reference_id'
:foreign_key => 'citation_id'
Book, Chapter, Article all have:
has_many :publications :as =>writing
I find this a bit confusing so any suggestions that would help clarify it would be great. Even object and field naming suggestions.
[I asked a less clear version of this question here.]
I also probably need to use has many through because I will need the ability to destroy the relationship
Here's a solution using a self-referential relationship using single table inheritance. Use these commands to create the app:
$ rails myproject
$ cd myproject
$ script/generate model publication type:string name:string
$ script/generate model citation publication_id:integer reference_id:integer
The setup the relationships this way:
class Publication < ActiveRecord::Base
has_many :citations
has_many :cited_publications, :through => :citations, :source => :reference
has_many :references, :foreign_key => "reference_id", :class_name => "Citation"
has_many :refered_publications, :through => :references, :source => :publication
end
class Citation < ActiveRecord::Base
belongs_to :publication
belongs_to :reference, :class_name => "Publication"
end
class Article < Publication
end
class Book < Publication
end
class Chapter < Publication
end
Now we can create the DB and try it out from the console:
$ rake db:migrate
$ script/console
Loading development environment (Rails 2.2.2)
>> a = Article.create!(:name => "Article")
=> #<Article id: 1, ...>
>> b = Book.create!(:name => "Book")
=> #<Book id: 2, ...>
>> a.citations.create(:reference => b)
=> #<Citation id: 1, publication_id: 1, reference_id: 2, created_at: "2009-02-15 14:13:15", updated_at: "2009-02-15 14:13:15">
>> a.citations
=> [#<Citation id: 1, ...>]
>> a.references
=> []
>> b.citations
=> []
>> b.references
=> [#<Citation id: 1, publication_id: 1, reference_id: 2, created_at: "2009-02-15 14:13:15", updated_at: "2009-02-15 14:13:15">]
>> a.cited_publications
=> [#<Book id: 2, type: "Book", name: "Book", created_at: "2009-02-15 14:11:00", updated_at: "2009-02-15 14:11:00">]
>> a.refered_publications
=> []
>> b.cited_publications
=> []
>> b.refered_publications
=> [#<Article id: 1, type: "Article", name: "Article", created_at: "2009-02-15 14:10:51", updated_at: "2009-02-15 14:10:51">]
Here's a solution that doesn't use Single Table Inheritance for the publications. That means that there are articles, books and chapters tables, instead of one publications table. Here are the commands to run to create the app:
$ rails myproject
$ cd myproject
$ script/generate model book name:string
$ script/generate model chapter name:string
$ script/generate model article name:string
$ script/generate model citation publication_type:string publication_id:integer reference_type:string reference_id:integer
Create this file in lib/acts_as_publication.rb:
module ActsAsPublication
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_publication
has_many :citations, :as => :publication
has_many :references, :as => :reference, :class_name => "Citation"
end
end
end
Create this file in config/initializers/acts_as_publication.rb:
ActiveRecord::Base.send(:include, ActsAsPublication)
Then call that in each model, Article, Book and Chapter, like this:
class Article < ActiveRecord::Base
acts_as_publication
end
Then add these relationships in app/models/citation.rb:
class Citation < ActiveRecord::Base
belongs_to :publication, :polymorphic => true
belongs_to :reference, :polymorphic => true
end
Now we can create the DB and try it out from the console:
$ rake db:migrate
$ script/console
Loading development environment (Rails 2.2.2)
>> a = Article.create!(:name => "a")
=> #<Article id: 1, ...>
>> b = Article.create!(:name => "b")
=> #<Article id: 2, ...>
>> Citation.create!(:publication => a, :reference => b)
=> #<Citation id: 1, publication_type: "Article", publication_id: 1, reference_type: "Article", reference_id: 2, created_at: "2009-02-15 13:14:27", updated_at: "2009-02-15 13:14:27">
>> a.citations
=> [#<Citation id: 1, ...>]
>> a.references
=> []
>> b.citations
=> []
>> b.references
=> [#<Citation id: 1, ...>]
>> Book.create!(:name => "foo")
=> #<Book id: 1, name: "foo", created_at: "2009-02-15 13:18:23", updated_at: "2009-02-15 13:18:23">
>> a.citations.create(:reference => Book.first)
=> #<Citation id: 2, publication_type: "Article", publication_id: 1, reference_type: "Book", reference_id: 1, created_at: "2009-02-15 13:18:52", updated_at: "2009-02-15 13:18:52">
>> Book.first.references
=> [#<Citation id: 2, ...>]
>> a.citations
=> [#<Citation id: 1, publication_type: "Article", publication_id: 1, reference_type: "Article", reference_id: 2, created_at: "2009-02-15 13:14:27", updated_at: "2009-02-15 13:14:27">, #<Citation id: 2, publication_type: "Article", publication_id: 1, reference_type: "Book", reference_id: 1, created_at: "2009-02-15 13:18:52", updated_at: "2009-02-15 13:18:52">]
I have an incomplete answer over at http://github.com/francois/so-536261/tree/master
Basically, the DB schema does support your use case, but ActiveRecord doesn't. The solution will probably involve using find by sql or other tricks.