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.
Related
Can you please help me in thinking sphinx?
Relationship:
Continent
has_many :countries
has_many :country_reports, through: :countries, :class_name => "Report"
has_many :publishers, through: :country_reports
Expected Output:
I want to find Continent.first.publishers
Please tell me how to write this in thinking sphinx rails
As I answered on the Thinking Sphinx Google group:
Because you’re searching on publishers, it’s the Publisher index you’ll need to modify to get this to work. I’m presuming a publisher belongs_to :country_report, country report belongs_to :country, and country belongs_to :continent.
If you’re using SQL-backed indices (using the :with => :active_record option), then you’ll want the following in your Publisher index:
has country_report.country.continent_id, :as => :continent_id
If you’re using real-time indices (:with => :real_time), then it’s the same, but you must specify the type as well:
has country_report.country.continent_id, :as => :continent_id, :type => :integer
However, if there’s a has_many or has_and_belongs_to_many instead of belongs_to in that chain of associations from publisher to continent, then it’s a little more complicated. Also, in this case, it might be possible to have more than one continent for a publisher, so we’re dealing with multiple values here.
For SQL-backed indices, a minor change, alter the associations chain:
has country_reports.country.continent_id, :as => :continent_ids
But with a real-time index, it’s better to have a method on the Publisher model that returns the necessary value or values, and then use that in the index:
# in app/models/publisher.rb
def continent_ids
country_reports.collect(&:country).collect(&:continent_id)
end
# in app/indices/publisher_index.rb
has continent_ids, :type => :integer, :multi => true
And then, once you’ve rebuilt your index, you can search as follows (with the attribute being plural if appropriate):
Publisher.search “foo”, :with => {:continent_id => continent.id}
This may work as well (the multiple levels of associations may confuse things though):
continent.publishers.search “foo”
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.
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.
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