I'm fairly new and am coming across a problem as I progress in the application I'm working on.
I have the following relationships set up
game
belongs_to :challenge
has_many :game_players, through: :playersessions, source: :user
has_many :playersessions
challenge
has_many :phrases
has_many :games
phrase
has_many :playedphrases
has_many :playersessions, through: :playedphrases
playedphrase
belongs_to :playersession
belongs_to :phrase
playersession
has_many :playedphrases
has_many :phrases, through: :playedphrases
All these relationships are working in my program, but I need to start doing some more advanced querying. For instance, I would like to find the remaining phrases. In english it would be "find this Game's Challenge's Phrases and remove the this Game's Playersession's Phrases". I believe the problem lies in the fact that #game.challenge.phrases returns Phrase objects and #game.playersessions must be iterated through to find all the phrase objects.
Any guidance on how to handle this type of query? Please let me know if any other info would help out.
I finally figured out a way to make this work - I don't know if it is the most eloquent ruby, but it works.
def determine_remaining_phrases
remain = #game.challenge.phrases
#playersessions.each do |session|
remain -= session.phrases
end
return remain
end
Basically I needed to iterate through the playersessions and remove the associated phrases each pass through. This leaves me with an array of phrase objects. If someone has a more eloquent answer, please let me know.
Related
Not sure if im overthinking this but would like some guidance and advice on this scenario.
I have two applications, one which you can log into and perform your basic CRUD, i.e create blog posts and the second a view of the same application, but no ability to log into and no ability to create a blog post. The second application would read from the same database as the first.
My question is how do i get the two applications reading from the same model in development and do i still need to create my models with columns etc in the view only app?
Example
App 1 (With CRUD)
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :category
belongs_to :user
has_many :images, as: :imageable, :dependent => :destroy
accepts_nested_attributes_for :images
attr_accessible :comments, :title, :category_id, :user_id, :image_id, :images_attributes, :imageable_id, :imageable_attributes, :slug
#Validations
validates :comments, :presence => {:message => 'Add your Comments'}
validates :title, :presence => {:message => 'Add your Title'}
#scopes
scope :latest_posts, :order => "posts.created_at DESC"
#scope :ruby_posts, :include => :category, :conditions => {"categories.name" => "Ruby"}, :order => "posts.created_at DESC"
def self.search(search)
where("title like ?", "%#{search}%")
end
end
App 2 (No Crud)
class Post < ActiveRecord::Base
#do i still provide all associations and attributes here?
end
I would really appreciate an explanation of what is going on in this kind of setup
thanks
You will need to have your models either shared or duplicated between the two applications. This means your Post example for App 2 would need to have the associations, scopes, and methods.
I have done this once before by moving all of the model classes into a gem that is included into both projects. This was actually pretty easy to do.
You do not need to share migrations though. If they are pointing to the same database, migrations should only live in one app, probably the one doing the writing. I wouldn't even let db/schema be checked in on App 2 and would maybe go further and disable rake db:* tasks.
Even if you move your models into a shared gem, you might want your "read only" app to enforce its read-only behavior by clearing permissions to assign attributes (attr_accessible and accepts_nested_attributes_for) or somehow preventing ActiveRecord models from saving in its environment. One quick and dirty way would be to monkey patch ActiveRecord::Base#save in an initializer for App 2 and have it do nothing or raise an error.
I have a set of models that's exactly like the example in railsguides:
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, through: :sections
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
They mentioned you can do this #document.paragraphs, which uses JOIN, but you can't go in the reverse direction... #paragraph.document just doesn't work. I'm aware of using delegate, but it still uses the same amount of queries.
Is there a way I can do this with joins() or includes() or something? What is the best way to handle an association like this?
In your controller, let's say you're querying for a collection of documents. Then it's important that you use includes to eager load the two associations you're going through:
#paragraphs = Paragraph.includes(:section => :document).where(:attribute => attribute)
And then in your view you can do this without fear of doing too many queries:
<% #paragraphs.each do |paragraph| %>
<%= paragraph.section.document %>
<% end %>
If you wanna use delegate to make it even cleaner and be able to write paragraph.document, you'll still benefit from the eager loading.
This is a follow up to Creating "feeds" from multiple, different Rails models. In this question, tadman suggests this method of creating a user feed of recent items from three models (Ticket, Post, Report):
#items = [ Ticket, Post, Report ].inject([ ]) do |a, with_class|
a + with_class.find(:all, :limit => 10, :order => 'created_at DESC')
end.sort_by(&:created_at).reverse[0, 10]
He suggests this as a method that will work, but that won't necessarily be the most efficient. He suggests as well than an alternative method would be to "create an index table that's got a polymorphic association with the various records."
I'm really interested in learning more about this alternative solution, it seems both more efficient and more elegant. Can anyone tell me how one would do this? Let's use the same background info from the last question as a base.
What I did once was, have a separate model Feed (feeds_controller) and update it in after_save callbacks to all the interesting models. So for example if you have a model Article, have an after_save callback:
def after_save
feed = Feed.new
feed[:model_name] = 'Article'
feed[:item_id] = id
feed.save
end
then, you can access the feed linearly just like any other model. The computational expense is incurred when saving the the feed, not reading from the feed.
Oh, you can also have Feed has_many :article; has_many :user, has_many :status and so forth, and then :include all those resources in the feed, and render them in views. Hope this makes sense ;-)
I have two models in a 1:n relation. Both are put out in JSON only. Therefore I defined as_json in both models:
class Foo < ActiveRecord::Base
has_many :foos, dependent: :destroy
def as_json options={}
super except: [:created_at, :updated_at, :id, :user_id], include: options[:include]
end
end
class Bar < ActiveRecord::Base
belongs_to :foo
def as_json options={}
super except: [:id, :foo_id, :created_at, :updated_at], include: options[:include]
end
end
Now I request foo's and bar's independently and as_json works as excepted. But in case I request bar's with foo's included the response contains foo's with attributes that should not be there. In fact as_json is not even called.
This seems to be standard behaviour. But how can I turn it off / achive my goal to always use as_json independently, wether include is used or not?
I appreciate every kind of hint, link or answer that helps solving this question.
Thx in advance.
Felix
Hey I have a problem with the extension of the existing dependency models. Well, according to
between the models are as follows:
I have a User model:
class User <ActiveRecord::Base
has_many :words, :through => :memo_words
has_many :memo_words, :dependent => :destroy
end
class MemoWord
belongs_to :user
belongs_to :word
end
class Word
has_many :translations, :dependent => :destroy
has_many :memo_words, :dependent => :destroy
end
class Translation
belongs_to :word
end
This is a diagram now:
http://img221.imageshack.us/img221/4232/przedik.png
Word model represents a word in one language and the model represents the Translation translation of individual words. I want to resolve the situation when a record in the table and the Word, there is no record Translation (word has no translation). I want to allow for user to add their own translations, but translations done by adding a local (per user). Due to the lack of relationship between Translation and User, the User is not possible to add words. And I question whether a good solution is to add the model UserTranslation:
UserTranslation class
belongs_to :word
belongs_to :user
end
And diagram with situation after change.
http://img851.imageshack.us/img851/7269/75031527.png
Which would have the same functionality as the model of Translation. In practice, I would have to copy the model to UserTranslation Translation by adding only 'belongs_to :user'. Is there a better approach to the problem
I would suggest that in your current scheme consider UserTranslations to be STI for Translations so -
class UserTranslation < Translation
belongs_to :user
end
This way all user translated words will be saved inside "translations" table but with type "user_translations". Then you may make id unapproved by default and build admin side approval functions.
This way, #word.translation would yield either translation or user_translation object.