activerecord

Yii Relational record with condition from other table

醉酒当歌 提交于 2019-12-09 11:10:39
问题 I have a relational record similar to the following: There are users (table users) with ids There are categories (table categories) with id, name There are articles (table articles) with id, body, category_id Then there is a table read_articles with article_id, user_id So a user can see a list of all articles in a category. They can click on an article and it will add an entry (user_id, article_id) to the read_articles table. I want to be able to use Yii's ActiveRecord to get all articles

Yii2: How to set default attribute values in ActiveRecord?

江枫思渺然 提交于 2019-12-09 08:46:50
问题 This may seem like a trivial question, however all of the obvious solutions that I can think of have their own flaws. What we want is to be able to set any default ActiveRecord attribute value for new records only, in a way that makes it readable before and during validation and does not interfere with derived classes used for search. The default values need to be set and ready as soon as we instantiate the class, so that (new MyModel)->attr returns the default attr value. Here are some of

query where date = Date.today with Rails, MySQL, and Active Record

帅比萌擦擦* 提交于 2019-12-09 08:44:08
问题 I see in the Active Record docs, you can query for a date using a greater than / less than comparison. However, what if you want to select where date = Date.today or must I query where date is greater than yesterday and less than tomorrow? As you can see, I'm doing exactly that in the following queries and querying where Date = today returns an empty set 1.9.3p286 :096 > Subscription.where("created_at = ?", Date.today).count (0.5ms) SELECT COUNT(*) FROM `subscriptions` WHERE (created_at =

CodeIgniter single update query to update one column with an existing columns data?

旧街凉风 提交于 2019-12-09 08:13:55
问题 In my db I have a Last and a Current column for datetime. Just to keep an eye on when someone last used a valid login to the service I am building up. What I want to know is, is it possible to update one column on a row with another column from that row, while updating the other column at the same time Ie: Set the Last as the current Current, then set the Current with the date thats right now essentially? Make any sense? 回答1: Try like this .. $data=array('current_login'=>date('Y-m-d H:i:s'));

Upsert in Rails ActiveRecord

强颜欢笑 提交于 2019-12-09 08:02:20
问题 Does ActiveRecord have a built-in upsert functionality? I know I could write it myself but I obviously don't want to if such a thing already exists. 回答1: Model.find_or_initialize likely does what you want. You can chain it with save or update_attributes if that makes sense. More info in the Rails Guides. 回答2: I just ran across this library: https://github.com/seamusabshere/upsert I haven't tested it yet, but it looks promising 回答3: There is an awesome new feature in rails 6: they added upsert

Retrieve all posts where the given user has commented, Ruby on Rails

我与影子孤独终老i 提交于 2019-12-09 07:12:53
问题 I have users, posts and comments. User can post only one comment to each post. class User < ActiveRecord::Base has_many :posts has_many :comments end class Post < ActiveRecord::Base has_many :comments belongs_to :user end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end On userpage ( http://host/users/1 for example) I want to show all posts where the given user has commented. Each post then will have all other comments. I can do something like this in my User

How to make attribute setter send value through SQL function

∥☆過路亽.° 提交于 2019-12-09 06:07:32
问题 I'm trying to make an attribute setter in an ActiveRecord model wrap its value in the text2ltree() postgres function before rails generates its sql query. For example, post.path = "1.2.3" post.save Should generate something like UPDATE posts SET PATH=text2ltree('1.2.3') WHERE id = 123 # or whatever What's the best way of doing this? 回答1: EDIT: To achieve exactly what you are looking for above, you'd use this to override the default setter in your model file: def path=(value) self[:path] =

Standalone ruby — How to load different environments from database.yml

孤街醉人 提交于 2019-12-09 05:44:26
问题 I have a background process that modifies records in a database. The models connect to the database using something like this: dbconfig = YAML::load(File.open('database.yml')) ActiveRecord::Base.establish_connection(dbconfig["development"]) class Clcar < ActiveRecord::Base .... end All model classes have those lines at the top. I agree that's a bad way of doing it. Is there a better way to get a connection to a model class? How do I pass the connection to the model? I want to be able to run

validate and update single attribute rails

一曲冷凌霜 提交于 2019-12-09 05:39:14
问题 I have the following in my user model attr_accessible :avatar, :email validates_presence_of :email has_attached_file :avatar # paperclip validates_attachment_size :avatar, :less_than => 1.megabyte, :message => 'Image cannot be larger than 1MB in size', :if => Proc.new { |imports| !imports.avatar_file_name.blank? } in one of my controllers, I ONLY want to update and validate the avatar field without updating and validating email . How can I do this? for example (this won't work) if @user

Rails/ActiveRecord has_many through: association on unsaved objects

岁酱吖の 提交于 2019-12-09 05:37:46
问题 Let's work with these classes: class User < ActiveRecord::Base has_many :project_participations has_many :projects, through: :project_participations, inverse_of: :users end class ProjectParticipation < ActiveRecord::Base belongs_to :user belongs_to :project enum role: { member: 0, manager: 1 } end class Project < ActiveRecord::Base has_many :project_participations has_many :users, through: :project_participations, inverse_of: :projects end A user can participate in many projects with a role